Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why wont this script play an animation?

Asked by 7 years ago

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

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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.
0
I fixed a typo. User#11440 120 — 7y
Ad

Answer this question