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

This animation wont play on Equipped?

Asked by 8 years ago
local tool = script.Parent
local humanoid = tool.Parent:FindFirstChild("Humanoid")
local anim = humanoid:LoadAnimation(tool.Animation)

function onEquipped()
    anim:Play()
end

function onUnequipped()
    anim:Stop()
end

tool.Equipped.connect(onEquipped)
tool.Unequipped.connect(onUnequipped)

Could someone help me out?

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago

When the script first runs, the parent of the tool will be Backpack. This means that there won't be a Humanoid in it. You really have two options to fix this:


Wait until the tool is equipped to do the actions of line 2 and 3. This will mean that now the tool's parent is your Character, so there will be a Humanoid in it.

function onEquipped()
    local hum = tool.Parent:WaitForChild("Humanoid")
    anim = hum:LoadAnimation(tool.Animation)
    anim:Play()
end

The second option is to use the LocalPlayer and get the Humanoid from there.

local tool = script.Parent
local plr = game.Players.LocalPlayer
    repeat wait() until plr.Character
local chr = plr.Character
local hum = chr:WaitForChild("Humanoid")
local anim = hum:LoadAnimation(tool.Animation)
0
Thanks so much! alonzo12345 60 — 8y
Ad

Answer this question