Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local physicsservice = game:GetService("PhysicsService")
- local serverstorage = game:GetService("ServerStorage")
- local replicatedstorage = game:GetService("ReplicatedStorage")
- local functions = replicatedstorage:WaitForChild("Functions")
- local requestTowerFuntion = functions:WaitForChild("RequestTower")
- local event = replicatedstorage:WaitForChild("Events")
- local spawntowerFuntion = functions:WaitForChild("SpawnTower")
- local animatetowerevent = event:WaitForChild("AnimateTower")
- local maxTowers = 10
- local tower = {}
- --------------------------------------------
- local function FindNearestTarget(newTower,range)
- local nearestTarget = nil
- for i, target in ipairs(workspace.Grassland.Mob:GetChildren()) do
- local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
- if distance < range then
- nearestTarget = target
- range = distance
- end
- end
- return nearestTarget
- end
- function tower.Attack(newTower ,player)
- local config = newTower.Config
- local target = FindNearestTarget(newTower,config.Range.Value)
- if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
- local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
- newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
- animatetowerevent:FireAllClients(newTower, "Attack")
- target.Humanoid:TakeDamage(config.Damage.Value)
- if target.Humanoid.Health <= 0 then
- player.Gold.Value += target.Humanoid.MaxHealth
- end
- task.wait(config.Cooldown.Value)
- end
- task.wait(0.1)
- if newTower and newTower.Parent then
- tower.Attack(newTower, player)
- end
- end
- ---------------------------------------------
- function tower.Spawn(player, name, cframe, previous)
- local allowedToSpawn = tower.CheckSpawn(player, name)
- if allowedToSpawn then
- local newTower
- if previous then
- previous:Destroy()
- newTower = replicatedstorage.Towers.Upgrades[name]:Clone()
- else
- newTower = replicatedstorage.Towers[name]:Clone()
- end
- newTower.HumanoidRootPart.CFrame = cframe
- newTower.Parent = workspace.Grassland.Tower
- newTower.HumanoidRootPart:SetNetworkOwner(nil)
- local bodyGyro = Instance.new("BodyGyro")
- bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
- bodyGyro.D = 0
- bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
- bodyGyro.Parent = newTower.HumanoidRootPart
- for i, object in ipairs(newTower:GetDescendants()) do
- if object:IsA("BasePart") then
- physicsservice:SetPartCollisionGroup(object, "Tower")
- end
- end
- player.Gold.Value -= newTower.Config.Price.Value
- player.PlacedTowers.Value +=1
- coroutine.wrap(tower.Attack) (newTower ,player)
- return newTower
- else
- warn("Requested mob dose not exist",name)
- return false
- end
- end
- spawntowerFuntion.OnServerInvoke = tower.Spawn
- function tower.CheckSpawn(player, name)
- local towerExists = replicatedstorage.Towers:FindFirstChild(name, true)
- if towerExists then
- if towerExists.Config.Price.Value <= player.Gold.Value then
- print("okkkkkkkkkkkkkkkkkkkkkkkkkkkkkk")
- if player.PlacedTowers.Value < maxTowers then
- return true
- end
- end
- end
- return false
- end
- requestTowerFuntion.OnServerInvoke = tower.CheckSpawn
- return tower
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement