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 spawntowerevent = event: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) tower.Attack(newTower ,player) end --------------------------------------------- function tower.Spawn(player, name, cframe) local allowedToSpawn = tower.CheckSpawn(player, name) if allowedToSpawn then local newTower = replicatedstorage.Towers[name]:Clone() 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) else warn("Requested mob dose not exist",name) end end spawntowerevent.OnServerEvent:Connect(tower.Spawn) function tower.CheckSpawn(player, name) local towerExists = replicatedstorage.Towers:FindFirstChild(name) 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