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 event = replicatedstorage:WaitForChild("Events")
- local spawntowerevent = event:WaitForChild("SpawnTower")
- local animatetowerevent = event:WaitForChild("AnimateTower")
- local tower = {}
- --------------------------------------------
- local function FindNearestTarget(newTower)
- local maxDistance = 20
- local nearestTarget = nil
- for i, target in ipairs(workspace.Grassland.Mob:GetChildren()) do
- local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
- if distance < maxDistance then
- nearestTarget = target
- maxDistance = distance
- end
- end
- return nearestTarget
- end
- function tower.Attack(newTower)
- local target = FindNearestTarget(newTower)
- 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(25)
- end
- task.wait(1)
- return tower.Attack(newTower)
- end
- ---------------------------------------------
- function tower.Spawn(player, name, cframe)
- local towerExists = replicatedstorage.Towers:FindFirstChild(name)
- if towerExists then
- local newTower = towerExists: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
- coroutine.wrap(tower.Attack) (newTower)
- else
- warn("Requested mob dose not exist",name)
- end
- end
- spawntowerevent.OnServerEvent:Connect(tower.Spawn)
- return tower
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement