I have an already working script that find the closest player and uses simple path to run to the closest player how would I code it to where when close to the player it will set the walk speed to 0 play a wind up animation and do the punch which deals the damage. Here is a script I already have tried.
local dummy = script.Parent
local windUp = script:WaitForChild("windUp")
local humanoid = script.Parent:WaitForChild("Humanoid")
local windAnim = humanoid.Animator:LoadAnimation(windUp)
local punch = script:WaitForChild("punch")
local punchAnim = humanoid.Animator:LoadAnimation(punch)
local animDebounce = false
local npc = script.Parent
closestPlayer = nil
closestDistance = 200
local afterFuncDist = nil
function getClosestPlayer()
for i, player in pairs(game:GetService("Players"):GetPlayers()) do
local Character = player.Character
local PrimaryPart = Character and Character.PrimaryPart
local hum = Character and Character:FindFirstChildOfClass("Humanoid")
if PrimaryPart and hum and (hum.Health > 0) then
local distance = (npc.PrimaryPart.Position - PrimaryPart.Position).Magnitude
if distance < closestDistance then
closestPlayer = Character
afterFuncDist = distance
end
end
end
end
if afterFuncDist <= 20 then
if animDebounce == false then
animDebounce = true
dummy:SetAttribute("Pathfinding", false)
dummy.Humanoid.JumpPower = 0
dummy.Humanoid.WalkSpeed = 0
windAnim:Play()
task.wait(1)
punchAnim:Play()
closestPlayer.Humanoid.Health = 0
dummy:SetAttribute("Pathfinding", true)
dummy.Humanoid.JumpPower = 50
dummy.Humanoid.WalkSpeed = 16
end
task.wait(3)
animDebounce = false
end
I am really confused why this isn’t working. Thanks for any help.