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

How can I make a tool script that plays an animation for the character using it?

I'm quite new to coding, and this is something I haven't been able to figure out for months, and none of the research I've been doing is helping.

My goal is to make a tool, (in this case, Ice Cream) and when you click with said tool equipped, it plays the animation I wish to play (in this case, the person taking a lick of the ice cream)

I've done research on various sites, and this is the code I came up with-

-- Import animation
local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/Asset?ID=1851375372"
 
-- Local variables
local animTrack = nil
local canPlay = true
 
function playShockAnim(Source)
	if canPlay then
		local player = game.Players.LocalPlayer.Character
		canPlay = false
		animTrack = player.Humanoid:LoadAnimation(animation) -- Load animation into Humanoid
		animTrack.KeyframeReached:connect(function(keyframeName) -- Bind function to KeyframeReached event
			local onActivation()
				canPlay = true
			end
		end)	
		animTrack:Play() -- Start the animation
	end
end

I tried my best to retain from errors, But so far, nothing happens when I equip the item and try to activate it. It would help if someone told me what I was doing wrong.

2 answers

Hello, guythatsrich!

I have written a script for you!

But first... you have to do a few things!

First, create an Animation inside your tool and change its ID.

Second, make sure its parent is the tool.

Third, name it to Anim! (otherwise, the script won't find it)

And, create a LocalScript inside your tool.

Animation = script.Parent.Anim
Human = game.Players.LocalPlayer.Character.Humanoid
AnimationTrack = Human:LoadAnimation(Animation)
Tool = script.Parent

Tool.Activated:Connect(function()
AnimationTrack:Play()
end)

Tell me if it doesn't work.

-- Good Luck!

---- AIphanium

2 comments

0

I'm afraid it didn't work. Your script didn't return any errors, but nothing happened.

0

I should warn you about something however... the animation will stop once your character starts walking, so... i have edited my script.

Ok I've been codding a sword yesterday and on that day i'v got the whole script down

so first your gonna need a tool then add the local Script inside the tool then add an "Animation" in side the localscript and then now type this below also make sure your animation id is inside the animation!

local play = game.Players.LocalPlayer
local use = false--I use this as my version of a debounce...U haven't use the item at the moment yet so that is why  "use" is set to false

script.Parent.Activated:Connect(function()
if use == false then
use = true--now you are using the item
local animation = play.Character.Humanoid:LoadAnimation(script.Animation)--change the name "Animation" to the name of your animation
animation:Play
wait(1)--This Number in here is basicly a cooldown number.Youu csn change it to whatever you want
use = false
end)

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