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

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

Asked by 5 years ago

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.

0
Equipped is an event, not a boolean theking48989987 2147 — 5y
0
Sorry, I did not know that. Please tell me what I can do. AwesomeMrBird -79 — 5y
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. ChefDevRBLX 90 — 5y
0
ChefDev, what I mean is when I take out the tool it plays an animation. AwesomeMrBird -79 — 5y

2 answers

Log in to vote
0
Answered by
pidgey 548 Moderation Voter
5 years ago
Edited 5 years ago

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!

0
How do I make the player frozen during the animation? AwesomeMrBird -79 — 5y
Ad
Log in to vote
1
Answered by
sheepposu 561 Moderation Voter
5 years ago

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

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. AwesomeMrBird -79 — 5y
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. pidgey 548 — 5y
0
??? AwesomeMrBird -79 — 5y
0
The problem is, what if the tool doesn't start in a player. What if the tool switches players, like a trade sheepposu 561 — 5y

Answer this question