while true do local p = game.Players:GetChildren() for i = 1,#p do if p[i].Character~=nil then if p[i].Character:findFirstChild("Torso")~=nil then if (p[i].Character.Torso.Position - script.Parent.Torso.Position).magnitude < 5 then script.Parent.Humanoid:LoadAnimation(script.Parent.Slash):Play() end end end end wait(1) end
This line is wrong:
script.Parent.Humanoid:LoadAnimation(script.Parent.Slash):Play()
You should load animations before you play them, like so:
local Animation = script.Parent.Humanoid:LoadAnimation(script.Parent.Slash)
And now, you can load the animation and it will work:
Animation:Play()
Final script:
local Animation = script.Parent.Humanoid:LoadAnimation(script.Parent.Slash) while true do local p = game.Players:GetChildren() for i = 1,#p do if p[i].Character~=nil then if p[i].Character:findFirstChild("Torso")~=nil then if (p[i].Character.Torso.Position - script.Parent.Torso.Position).magnitude < 5 then Animation:Play() end end end end wait(1) end
This should work. However, you should use a different method of getting and chasing players, because this one might play the animation over and over each second, without it waiting to finish. A good example would be making the mob animate attacks when he's close enough, in a separate loop, instead of the same loop where he's actively hunting people.