Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- MADE BY MRMRCODERKID ON YOUTUBE!
- https://www.youtube.com/channel/UCqIm_0rGy01QNvH5dU6hbyQ
- MODEL USED:
- https://create.roblox.com/store/asset/17638057115
- Looks for closest player
- Chase down closest player
- If distance is very close, Attack
- Else
- Continue chasing down
- --]]
- local my_char = script.Parent
- local my_root = my_char.HumanoidRootPart
- local my_humanoid = my_char.Humanoid
- local attack_cooldown = false
- function GetClosestPlayer()
- local selected_player = nil
- local potential_players = {}
- for i,p in game:GetService("Players"):GetPlayers() do
- if p.Character and p.Character.PrimaryPart ~= nil then
- table.insert(potential_players, p)
- end
- end
- for i, pPlayer in potential_players do
- local smallest_dist = math.huge
- local char = pPlayer.Character
- local root = char.PrimaryPart
- local root_rootDistance = (my_root.Position - root.Position).Magnitude
- if root_rootDistance < smallest_dist then
- smallest_dist = root_rootDistance
- selected_player = pPlayer
- end
- end
- return selected_player
- end
- function Attack(target_humanoid)
- target_humanoid:TakeDamage(10)
- end
- while true do
- task.wait()
- local target = GetClosestPlayer() or nil
- if target ~= nil then
- local target_character = target.Character
- local target_humanoid = target_character.Humanoid
- local target_root = target_character.PrimaryPart
- local root_rootDistance = (my_root.Position - target_root.Position).Magnitude
- if root_rootDistance < 3 then
- if attack_cooldown == false then
- Attack(target_humanoid)
- attack_cooldown = true
- task.delay(2, function()
- attack_cooldown = false
- end)
- end
- else
- my_humanoid:MoveTo(target_root.Position)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement