Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why wont this script play an animation?

I created a tool that's supposed to play an animation when activated, but it does not. Here is the code.

tool = script.Parent
handle = tool.Handle

function swing()
local player = tool.Parent
local humanoid = player.Humanoid
local swinganim = tool.handle.swinganim

local playAnim = humanoid:LoadAnimation(swinganim)
playAnim:Play()
wait(2)
playAnim:Stop()	
end

tool.Activated:connect(swing)

1 answer

✓ Accepted answer

Try using a Local Script.

I'm not the best at animations, but it's probably best to play them from a Local Script.

-- Local Script in tool
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local humanoid = char:WaitForChild("Humanoid")
local tool = script.Parent
local handle = tool:WaitForChild("Handle")
local swinganim = handle:WaitForChild("swinganim")

function swing()
	local playAnim = humanoid:LoadAnimation(swinganim)
	playAnim:Play()
	wait(2)
	playAnim:Stop() 
end

tool.Activated:connect(swing)-- Edit

Changes I made,

  • I used WaitForChild

Using WaitForChild is useful, especially when using a Local Script.

  • I used Local Variables

This doesn't really change much, but local variables are more useful and I like the way they look.

  • I moved all the variables outside the function

This causes less lag and is overall better. I would suggest getting into the habit of doing this yourself.

  • I obviously used a Local Script and used Local Player

This was my guess of the problem.


I hope I helped. If you have any questions fill free to ask me.

Good Luck!

If I helped, please don't forget to accept my answer.

1 comment

0

I fixed a typo.

This discussion is preserved as it was. New answers, comments, and votes are closed.