Function firing more than once

initially when unequipping the tool it prints “a” once, but unequipping it again makes it print “a” twice, and so on because of the unequip event being in an another event
my question here is how do i make it fire once without making significant changes to the script

local tool = script.Parent
local animScript = nil
local EQUIPPED = false

tool.Equipped:Connect(function()
if EQUIPPED == false then
EQUIPPED = true
local ANIMATION = tool.Parent:WaitForChild(“Humanoid”):LoadAnimation(tool:WaitForChild(“Animation”))
ANIMATION:Play()
print(“equipped”)

  tool.Unequipped:Connect(function()
  	EQUIPPED = false
  	ANIMATION:Stop()
  	print("a")
  end)

end
end)

Put the Unequipped event outside of the Equipped event and change some of t he code around

Should look like this

local tool = script.Parent
local animScript = nil
local EQUIPPED = false

local ANIMATION

tool.Equipped:Connect(function()
	if EQUIPPED == false then
		EQUIPPED = true
		if not ANIMATION then
			ANIMATION = tool.Parent:WaitForChild("Humanoid"):LoadAnimation(tool:WaitForChild("Animation"))
		end
		ANIMATION:Play()
		print("equipped")
	end
end)


tool.Unequipped:Connect(function()
	EQUIPPED = false
	ANIMATION:Stop()
	print("a")
end)
1 Like

I think it’s because you put the unequipped event in the equipped event. You should just move the unequipped event outside the equipped event.

Change tool.Unequipped:Connect(function() to tool.Unequipped:Wait()

1 Like