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

Why is a different animation playing, not the one defined in a script?

Hello. So basically i've made 2 animations. One called HatchetCut and other GrabAnim. So my aim is when i click with "Hatchet" tool an animation called "HatchetCut" should play and when i click my mouse with "Grab" tool equipped, an animation named "GrabAnim" should play. For some reason, when im using any of these tools, the animation of the tool which is first to enter the player's backpack will always play, even if im using the other tool.

Code:

HATCHET:

local pipp = game.Players.LocalPlayer
local Mouse = pipp:GetMouse()

---- Irrelevant Code ----

Mouse.Button1Down:connect(function(key)
	
	local s = pipp.Character.Humanoid:LoadAnimation(script.Parent:findFirstChild("HatchetCut"))
	s:Play()
end)

GRAB:

local pipp = game.Players.LocalPlayer
local Mouse = pipp:GetMouse()

---- Irrelevant Code ----

Mouse.Button1Down:connect(function(key)
	local s = pipp.Character.Humanoid:LoadAnimation(script.Parent:findFirstChild("GrabAnim"))
	s:Play()
end)

So what am i doing wrong?

2 comments

0

Try putting these inside of a "Equipped" event. It seems like it runs through one time. Maybe adding this to Equipped with redefine what happens when you click everytime you equip the other tool. Let me know if that works

0

Nope, doesnt work

1 answer

✓ Accepted answer

This code is running every time you click the mouse, so the last loaded animation will be the one to play.

You need to make it run only when the tool is equipped, example:

local pipp = game.Players.LocalPlayer
local Mouse = pipp:GetMouse()
local Tool = script.Parent -- Should be the tool. If not, make sure it is.
local Clicked -- The clicked event, here so we can disconnect it when we unequip
---- Irrelevant Code ----

Tool.Equipped:connect(function()
    Clicked = Mouse.Button1Down:connect(function(key)
		local s = pipp.Character.Humanoid:LoadAnimation(script.Parent:findFirstChild("GrabAnim"))
		s:Play()
    end)
end)


Tool.Unequipped:connect(function()
	Clicked:disconnect() -- This makes it so it doesnt fire multiple times every time we requip the tool and click.
	Clicked = nil
end)

9 comments

0

thats the thing, i need it to run when i click the mouse, not when its equipped. Thank you for the answer though

0

You cant have them both run when you click the mouse, though.

0

Still doesnt work.

0

Try it now. This should work, do only when clicked, but also only if you have the right tool equipped.

0

Works prefectly now! Thank you very much :)

0

How do i make this "Answered?"

0

I assume you click a button labled mark as answer, idk i dont usually ask questions on here

0

I must be blind since i dont see the button

0

No idea then.

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