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

How do I make a script in a tool that makes the tool play a animation once I equip it?

I have a tool in game.StarterPack I want it to play a animation once I equip it. I made a r6 animation and the game is r6. I have the animation in the tool. I do not know if I should use a server script or a local script. But, I made a server script that does not work. Here it is:

local animation = script.Parent.Animation


if script.Parent.Equipped == true then
	local animation:Play()
end

It does not work, please help me.

4 comments

0

Equipped is an event, not a boolean

0

Sorry, I did not know that. Please tell me what I can do.

0

Are you askng, like how to make a equip animation script? Like the way you hold the tool? or what? And also, as theking said, Equipped is an event.

0

ChefDev, what I mean is when I take out the tool it plays an animation.

2 answers

✓ Accepted answer

Equipped is an event. You can't compare Equipped (which is RBXScriptSignal) to a boolean value. Instead, connect it to a function so that when the event fires, the function is ran. You can connect an event to a function by using the Connect method in a RBXScriptSignal. Remember that you also can't play an animation if it's built on R15 and you're using an R6 rig.

local player = game.Players.LocalPlayer --this is the Local Player. only local script's can reference the Local Player.
local character = player.Character or player.CharacterAdded:wait() --if the Character is nil than the script will wait for the character to be added
local humanoid = character:WaitForChild("Humanoid") --wait for the humanoid to be added to the character before we try and reference it

local tool = script.Parent
local animation = tool.Animation

local track = humanoid:LoadAnimation(animation) --load the animation into the humanoid

tool.Equipped:Connect(function() --connect event to function
    wait(0.1) --allow some time. roblox doesn't like it when you play an animation immediately after this event apparently
    track:Play() --play the animation
end)
tool.Unequipped:Connect(function()
    track:Stop() --stop the animation in case the player unequips the tool while the equip animation is playing. counter-measure
end)

I tried to be descriptive on what was happening. It's good practice to use lots of descriptive variables. Good luck!

1 comment

0

How do I make the player frozen during the animation?

You can do this

local animation = script.Parent.Animation
local animation_time-span = 1 --seconds
local tool = script.Parent

script.Parent.Equipped:Connect(function()
	local humanoid = game.Players.LocalPlayer.Character:WaitForChild('Humanoid') --get humanoid (use LocalScript for this)
	humanoid = tool.Parent:WaitForChild('Humanoid') -- get humanoid (script, not localscript)
	local track = humanoid:LoadAnimation(animation)
	track:Play()
	wait(animation_time-span)
	track:Stop()
end)

Remove the wait and stop and it will play continuously

4 comments

0

I want a script that once I take out the tool from my backpack it plays an animation. The animation is in the tool.

0

Your variable at line 2 is a syntax error. You don't need to load the animation into the humanoid every time it's equipped - initialize it in the global scope.

0

???

0

The problem is, what if the tool doesn't start in a player. What if the tool switches players, like a trade

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