Effective way to disable tool during equip animation?

okay so, when making my weapon tool i quickly scripted this.

local eq = false -- unequipped by default
Tool.Equipped:Connect(function()
	equipA:Play()
	wait(1.1)
	eq = true -- marked as equipped after equip finished
	idleA:Play()
end)

Tool.Unequipped:Connect(function()
	idleA:Stop()
	equipA:Stop()
	eq = false -- marked as unequipped once again
end)

Tool.Activated:Connect(function()
	if eq == false then return end -- end if tool hasn't finished equipping
end)

now after testing i have realised my stupid mistake here. Tool.Equipped doesn’t stop after the tool has been unequipped. this equip animation lasts 1.1s, if you re-equip the tool 0.5s into the animation, the old Tool.Equipped function will continue and ‘eq’ will become true after 0.6s, in the middle of the freshly started animation. losing a braincell trying to figure out a workaround…

preventing the player from re-equipping by disabling tool bar is not a solution i’m looking for. players should be able to change weapon during the animation. checking IsPlaying of the animation is a option but it does not work very good for me, i need to change the equip time a bit and i end up with choppy animation interactions.

how could i get this to work properly?

One way is to have an “activeAnimation” variable that points to the currently playing animation. Then, check if it is playing before starting any new animations, otherwise wait. This will give you behavior approximately like how Zeppelin Wars does it, where even if you unequip a weapon its reload animation will keep playing, preventing you from equipping and firing another while still reloading.

1 Like

hi, thanks for your reply.
this indeed fixes eq becoming true despite tool being unequipped, however one problem stays, the one i mentioned in the post.

Tool.Equipped:Connect(function()
	equipA:Play()
	wait(1.1)
    if activeAnim ~= "equip" then return end
	eq = true
	idleA:Play()
end)

the active animation would probably be set to “none” once unequipped making so that it won’t set the idle and etc. prior to this post i did have a countermeasure for preventing the function’s continuation, which was checking if tool is a child of the character. but i removed it here to simplify the code, which wasn’t probably the smartest thing so my bad.

the real problem is what i mentioned in the post. if you equip the tool again, activeAnim becomes “equip” and the previous Tool.Equipped can continue after the wait, shortening the second equip’s time. i’m sorry if i’m bad at explaining this.

Keep track of the thread that runs when the equipped event fires and cancel it upon unequip.

local Tool = script.Parent

local fullyEquipped = false
local equippingThread: thread?

Tool.Equipped:Connect(function()
	equippingThread = coroutine.running()
	--Play equip animation
	task.wait(1.1)
	fullyEquipped = true
    equippingThread = nil
	--Play idle animation
end)

Tool.Unequipped:Connect(function()
	--Stop idle animation
	if equippingThread ~= nil then
		task.cancel(equippingThread)
		equippingThread = nil
		--Stop equipping animation
	end
	fullyEquipped = false
end)
2 Likes

The check should be before you start playing the new animation I think.

i haven’t worked much with threads before and had no idea you could do this.
thanks a lot, it’s simple and works perfectly.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.