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

Why doesn't my animation play after the remote event has been fired?

Basically I made a script so that when the remote is fired, it finds the player who fired it and plays an animation on them. For some reason it doesn't work and nothing shows up in the output.

local LA = game.ReplicatedStorage.LightAttack
local HA = game.ReplicatedStorage.HeavyAttack
local swordanim = game.ReplicatedStorage.Animation1

LA.OnServerEvent:Connect(function(plr)
local player = plr
local char = player.Character or player.CharacterAdded:Wait()
local hum = plr:WaitForChild('Humanoid', 10000)
local anim = hum:LoadAnimation(swordanim)
anim:Play()
print'works'
end)

HA.OnServerEvent:Connect(function(plr)
local name = plr.Name
local player = game.Workspace:FindFirstChild(name)
local hum = player:FindFirstChild('Humanoid')
end)

1 answer

It's not working as you're waiting 10,000 seconds for the Humanoid to appear in the Player. The Humanoid is located in the Character. I'd recommend not doing :WaitForChild(Child, time) as like you've just encountered, your code won't error even though it's wrong because it first has to wait 10,000 seconds just in-case it loads in the Player which it never will.

I've fixed the code for you.

local LA = game.ReplicatedStorage.LightAttack
local HA = game.ReplicatedStorage.HeavyAttack
local swordanim = game.ReplicatedStorage.Animation1
LA.OnServerEvent:Connect(function(plr)
    local player = plr
    local char = player.Character or player.CharacterAdded:Wait()
    local hum = char:WaitForChild('Humanoid')
    local anim = hum:LoadAnimation(swordanim)
    anim:Play()
    print'works'
end)
HA.OnServerEvent:Connect(function(plr)
    local name = plr.Name
    local player = game.Workspace:FindFirstChild(name)
    local hum = player:FindFirstChild('Humanoid')
end)

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