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:
1 | local animation = script.Parent.Animation |
2 |
3 |
4 | if script.Parent.Equipped = = true then |
5 | local animation:Play() |
6 | end |
It does not work, please help me.
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.
01 | local player = game.Players.LocalPlayer --this is the Local Player. only local script's can reference the Local Player. |
02 | local character = player.Character or player.CharacterAdded:wait() --if the Character is nil than the script will wait for the character to be added |
03 | local humanoid = character:WaitForChild( "Humanoid" ) --wait for the humanoid to be added to the character before we try and reference it |
04 |
05 | local tool = script.Parent |
06 | local animation = tool.Animation |
07 |
08 | local track = humanoid:LoadAnimation(animation) --load the animation into the humanoid |
09 |
10 | tool.Equipped:Connect( function () --connect event to function |
11 | wait( 0.1 ) --allow some time. roblox doesn't like it when you play an animation immediately after this event apparently |
12 | track:Play() --play the animation |
13 | end ) |
14 | tool.Unequipped:Connect( function () |
15 | track:Stop() --stop the animation in case the player unequips the tool while the equip animation is playing. counter-measure |
16 | end ) |
I tried to be descriptive on what was happening. It's good practice to use lots of descriptive variables. Good luck!
You can do this
01 | local animation = script.Parent.Animation |
02 | local animation_time-span = 1 --seconds |
03 | local tool = script.Parent |
04 |
05 | script.Parent.Equipped:Connect( function () |
06 | local humanoid = game.Players.LocalPlayer.Character:WaitForChild( 'Humanoid' ) --get humanoid (use LocalScript for this) |
07 | humanoid = tool.Parent:WaitForChild( 'Humanoid' ) -- get humanoid (script, not localscript) |
08 | local track = humanoid:LoadAnimation(animation) |
09 | track:Play() |
10 | wait(animation_time-span) |
11 | track:Stop() |
12 | end ) |
Remove the wait and stop and it will play continuously