I’m currently making a knife for my game, and I need help with making a custom equip animation script for it.
I’m trying to use YouTube for scripting help, since I am terrible at scripting, but there were no videos on this topic.
Does anybody have a solution to this?
3 Likes
local weapon = --directory to weapon
weapon.Equipped:Connect(function() --when your knife is equiped, it will do the things below.
--load animation here
--play anmation here
end)
Script should be a LocalScript and placed anywhere, preferable in the knife Tool if plan use to use script.Parent
for weapon val.
You can learn more about animations here.
1 Like
I can’t seem to figure out how to load the animation…
Put a local script inside your knife and write this
local knife = script.Parent – here we got the knife tool
local pla = game.Players.LocalPlayer --here we got the player
local cha = pla.Character --here we got the character from the player
local hum = cha:WaitForChild(“Humanoid”) --here we got the humanoid from the character
local animation = Instance.new(“Animation”) --here we create a new animation
animation.Parent = script.Parent --here we put the new animation inside the knife tool
animation.AnimationId = "rbxassetid:/–Inside here " – put your animation id here
local animTrack = hum:LoadAnimation(animation) --here we load the animation
knife.Equipped:Connect(function() --here we make the animation play when we equip the knife
animTrack:Play() --here the animation plays
end)
knife.Unequipped:Connect(function() --here we make the animation stop when unequipped
animTrack:Stop() --here the animation stops
end)
Hope this helps you :).
4 Likes
I got this error on line 4:
Attempt to index nil with “WaitForChild”.
edit: Could you also add a sound effect into the code? I’m trying to put a sound effect in there as well.
Please take note that Humanoid:LoadAnimation()
is now deprecated. You should use Animator instead, which is a child of the humanoid.
Wait. The error was on this line:
local hum = cha:WaitForChild("Humanoid")
Maybe the character hasn’t been loaded yet, you should put a pla.CharacterAdded:Wait()
in your char variable.
1 Like
Oh right. The character might be nil…
That fixed the issue! Thanks for the help. The animation works now.
1 Like
But, I still need to add a sound effect into the script.
Could you help with that too?
For the sound i used a script when i made a flash light.
you can insert a sound into the Handle and change the soundId, then insert a script into the knife tool and write something like this:
local knife = script.Parent
local function toolActivated()
Knife.Handle.Sound:Play()
end
knife.Activated:Connect(toolActivated)
wait, you want to play the sound when the knife is equipped or activated?
I want it to play when it is equipped.
then change it to this
local knife = script.Parent
local function toolEquipped()
Knife.Handle.Sound:Play()
end
knife.Equipped:Connect(toolActivated)
I fixed it myself actually. I did this:
line 3: local function equip()
line 7: knife.Equipped:Connect(equip)
edit: unrelated, but now all I gotta do is to make a script so the knife actually works.
3 Likes