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

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

Asked by 5 years ago

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

Log in to vote
0
Answered by
AIphanium 124
5 years ago

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

0
I'm afraid it didn't work. Your script didn't return any errors, but nothing happened. guythatsrich 0 — 5y
0
I should warn you about something however... the animation will stop once your character starts walking, so... i have edited my script. AIphanium 124 — 5y
Ad
Log in to vote
0
Answered by
Tizzel40 243 Moderation Voter
5 years ago

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)

Answer this question