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

LocalScript for playing animation when tool is clicked doesn't work? [ANSWERED]

I have a tool with a localScript inside and an Animation with my animation Id inputted in properties already.

local cd = false

script.Parent.Activated:Connect(function()
	if cd == false then
		cd = true
		game.Workspace.Events.MainEvent.AddTrust:FireServer()
		local Player = game.Players.LocalPlayer
		local Character = Player.Character
		local Humanoid = Character:WaitForChild("Humanoid")
		local anim = script.Parent.Animation
		local Track = Humanoid:LoadAnimation(anim)
		Track:Play()
		Track:Stop()
		script.Parent.Enabled = false
		wait(5)
		script.Parent.Enabled = true
		cd = false
	end
end)

When the tool is clicked then fire a RemoteEvent (unrelated) and play the animation, wait for it to finish playing then stop. However, this script doesn't work. No error found in output

2 comments

0

try removing line 13, it stops the animation immediatly after it starts to play

0

Thanks, Removing line 13 worked!

1 answer

Disabling and enabling scripts is a bad habit, especially when you get into a loophole that involves disabling the script and not realising that you can enable it again. Simply remove the lines that disable then enable the script. It's not working because once you disable it, it won't continue to get to the line which enables it again. Also there is no need to disable a script the debounce should work.

local cd = false

script.Parent.Activated:Connect(function()
    if cd == false then
        cd = true
        game.Workspace.Events.MainEvent.AddTrust:FireServer()
        local Player = game.Players.LocalPlayer
        local Character = Player.Character
        local Humanoid = Character:WaitForChild("Humanoid")
        local anim = script.Parent.Animation
        local Track = Humanoid:LoadAnimation(anim)
        Track:Play()
        Track:Stop()
        wait(5)
        cd = false
    end
end)

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