How to make it only once?

Hello! I want to make it so that when you touch a part, an animation plays and the GUI appears. I have it so that when you touch the part, you can’t move. I have also added a debounce to prevent the player from moving and sending multiple signals. However, with all this in place, the Touched event still happens multiple times, and it is messing with my GUI.

How do I fix this? Thanks

local repStorage = game.ReplicatedStorage
local db = false

script.Parent.Touched:Connect(function(part)
	db = true
	if part.Parent:FindFirstChild("Humanoid") and db then
		local plr = game.Players:GetPlayerFromCharacter(part.Parent)
		repStorage.Visible:FireClient(plr, true)
		repStorage.Dialogue:FireClient(plr, "end for rat king", 4, workspace["Rat King"], "how dare you enter my lair without my permission!! you shall pay for this..", "if you can get me 5 cheese in these sewers, then i shall spare you...", "if not, i guess you will see...", "mwahahahhahahahahahahahahah")
		task.wait(16)
		db = false
	end	
end)

because youre checking if the debounce is true, change this line:

to this:

	if part.Parent:FindFirstChild("Humanoid") and not db then

also everytime the part gets touched the debounce gets set to true even before checking if its false, move the db = true after the if statement,

this should be the fixed script:

local repStorage = game.ReplicatedStorage
local db = false

script.Parent.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") and not db then
		db = true
		local plr = game.Players:GetPlayerFromCharacter(part.Parent)
		repStorage.Visible:FireClient(plr, true)
		repStorage.Dialogue:FireClient(plr, "end for rat king", 4, workspace["Rat King"], "how dare you enter my lair without my permission!! you shall pay for this..", "if you can get me 5 cheese in these sewers, then i shall spare you...", "if not, i guess you will see...", "mwahahahhahahahahahahahahah")
		task.wait(16)
		db = false
	end	
end)
2 Likes

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