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

Why will this script only run once?

I have created a script that launches a localscript that creates a gui on the players screen, I have also attempted to create a debounce system to stop the players screen from being spammed with the same GUI, however it only allows the script to be run once, and never again. which is not what I needed, I do not know why the script does not run again after the player touches it a second time.

Have I done the debounce wrong? or is it something else?

local event = Instance.new("RemoteEvent",game.ReplicatedStorage)
event.Name = "conversation started"

local debounce = false
script.Parent.Touched:connect(function(hit)
	if not debounce then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			debounce = true
			event:FireClient(player,player)
		end
	end
end)
debounce = false

1 answer

✓ Accepted answer

You want to ensure that you are changing the debounce back to false as soon as you are done with whatever you are doing in the event. So you simply need to check at the beginning of the function as to whether the debounce is true and if so, return and stop the function immediately. From there, set it to true and do the work, and at the end of the work, set it to false. Done!

local event = Instance.new("RemoteEvent",game.ReplicatedStorage)
event.Name = "conversation started"

local debounce = false
script.Parent.Touched:connect(function(hit)
	if debounce then return end
	debounce = true

	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		event:FireClient(player,player)
	end

	debounce = false
end)

You'll then have to, in your client-side LocalScript, just make sure that the GUI doesn't exist before you create a new one.

7 comments

0

This is unfortunately still allowing the script to spam the players screen with guis, would I be right to assume that maybe the localscript also may require a debounce system to be included?

0

No. I think the best bet is to create another RemoteEvent, or use the same one, and fire it from the client to the server. If you do that, I can rewrite my answer to work with that. Because as of now, the server doesn't know when the client has closed the GUI.

0

OOORRR better idea. In the LocalScript, simply check if the GUI exists already before you create it again.

0

That second one sounds simple, I shall try that! I shall update with results!

0

Add a wait()

0

No don't do that, that's terrible coding and it'll only slow down the rapid showing of GUIs

0

Haha no it's okay I finally got it, I have no idea why it took me so long, it went wrong quite a bit and i began to doubt myself >_< but yes I've marked this as the answer, however the answer is in the comments

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