Tool firing multiple times

I’m making a quick game, where you basically throw chairs…

However I’ve run into a problem. My LocalScript detects all the tool equipped and gives them a tag
If the tool has that tag, it will have the function to throw the chair, however, when I unequip the tool, I aded a print() function to see if it’s working, and it’s printing multiple times, and I don’t know the cause?

function setupTool(toolPart)
	toolPart.Equipped:Connect(function()
		idleAnimationTrack:Play()
	end)

	toolPart.Unequipped:Connect(function()
		idleAnimationTrack:Stop()
		return warn("tool unequipped")
	end)

	mouse.Button1Down:Connect(function()
		if toolPart.Parent == character then
			if thrown == false then
				thrown = true
				throwbeginAnimationTrack:Play()
			end
		end
	end)

	mouse.Button1Up:Connect(function()
		if toolPart.Parent == character then
			if thrown == true then
				if throwAnimationTrack.IsPlaying == false then
					throwAnimationTrack:Play()
					throwbeginAnimationTrack:Stop()
				end
			end
		end
	end)

	throwAnimationTrack:GetMarkerReachedSignal("Throw"):Connect(function()
        thrown = false
		throwEvent:FireServer(mouse.Hit.Position, toolPart.Handle:FindFirstChild("FirePoint"),toolPart.Damage.Value, finalSpeed, toolPart.Handle, toolPart.Handle.Throw)
		throwAnimationTrack:Stop()
	end)
end

character.ChildAdded:Connect(function(child)
	if child:IsA("Tool") then
		child:AddTag("EquippedTool")
	end
end)

character.ChildRemoved:Connect(function(tool)
	if tool:IsA("Tool") then
		tool:RemoveTag("EquippedTool")
	end
end)

collectionService:GetInstanceAddedSignal("EquippedTool"):Connect(setupTool)

If somebody can help me out I will appreciate it!

2 Likes

From what I understood
is this the part that throws the Chair?

Well you can do Debounce

local Debounce = false

mouse.Button1Down:Connect(function()
	if toolPart.Parent == character then
		if thrown == false and Debounce == false then
			Debounce = true
			thrown = true
			throwbeginAnimationTrack:Play()
			task.wait(1) --Cooldown here
			Debounce = false
		else
			warn("On Cooldown")
		end
	end
end)
1 Like

this isn’t the issue, it’s about equipping the tool, it prints it multiple times and I don’t know why

2 Likes

Can you try this?

local connectedTools = {}

function setupTool(toolPart)
    if connectedTools[toolPart] then
        return  -- Already connected for this tool
    end

    toolPart.Equipped:Connect(function()
        idleAnimationTrack:Play()
    })

    toolPart.Unequipped:Connect(function()
        idleAnimationTrack:Stop()
        return warn("tool unequipped")
    end)

    mouse.Button1Down:Connect(function()
        if toolPart.Parent == character then
            if thrown == false then
                thrown = true
                throwbeginAnimationTrack:Play()
            end
        end
    })

    mouse.Button1Up:Connect(function()
        if toolPart.Parent == character then
            if thrown == true then
                if throwAnimationTrack.IsPlaying == false then
                    throwAnimationTrack:Play()
                    throwbeginAnimationTrack:Stop()
                end
            end
        end
    })

    throwAnimationTrack:GetMarkerReachedSignal("Throw"):Connect(function()
        thrown = false
        throwEvent:FireServer(mouse.Hit.Position, toolPart.Handle:FindFirstChild("FirePoint"),toolPart.Damage.Value, finalSpeed, toolPart.Handle, toolPart.Handle.Throw)
        throwAnimationTrack:Stop()
    end)

    connectedTools[toolPart] = true
end

character.ChildAdded:Connect(function(child)
    if child:IsA("Tool") then
        child:AddTag("EquippedTool")
        setupTool(child)
    end
end)

character.ChildRemoved:Connect(function(tool)
    if tool:IsA("Tool") then
        tool:RemoveTag("EquippedTool")
        connectedTools[tool] = nil
    end
end)

collectionService:GetInstanceAddedSignal("EquippedTool"):Connect(function(tool)
    setupTool(tool)
end)
1 Like

nope :frowning: , still doesnt work (characterss)

1 Like

Best I could recommend is Use ChatGPT or Roblox Assistant

Roblox Assitant:
image

ChatGPT before usage:

1 Like

maybe if i send you a video you will figure it out?
what do you say?

2 Likes

hardly read through your script, but it seems like the function is only firing if the tool is equipped…maybe modify it to fire when it is both equipped and unequipped? otherwise the unequipped function will never fire…or have it run outside the setupTool function?

let me know

1 Like

nope, should i make like a custom backpack? since I have another game with a custom backpack system and i figured it would work better if i did this, or should i do separate scripts for each tool?

1 Like

I think it’s because throwing the chair, which I’m guessing removes from your inventory, isn’t getting its tag removed. I know barely anything about tags, but my guess has to do with that area. It’s possible that the setupTool function gets fired multiple times, so that’s why the unequip is printing multiple times.

1 Like

I think we need vid + full script or something like that

I really have no idea why it prints multiple times since i don’t think it should happen with just a script like that

1 Like

as you can see, everytime I unequip and equip back the tool it will fire more times the amount

2 Likes

Thats looks so weird :joy: can you check if player character has the same chair on him multiple times?

1 Like

nope, the tool gets removed from the player just fine

1 Like

Something seems to stack it but got no idea

1 Like

should i just do a script in each tool? seems like it works just fine like this

1 Like

I really got no idea how to solve it, I would have checked it with edit perms but Even I am lazy to do something like that

1 Like

it seems to somehow stack no matter how i look

1 Like

really strange huh?

charschars

2 Likes

Does it happen after 1st throw or Always?
Maybe try debounce for equipping to make player not spam chairs