Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why will this script only run once?

Asked by
mrcool2 75
8 years ago

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

Log in to vote
1
Answered by 8 years ago

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.

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? mrcool2 75 — 8y
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. NoahWillCode 370 — 8y
0
OOORRR better idea. In the LocalScript, simply check if the GUI exists already before you create it again. NoahWillCode 370 — 8y
0
That second one sounds simple, I shall try that! I shall update with results! mrcool2 75 — 8y
View all comments (3 more)
0
Add a wait() NinjoOnline 1146 — 8y
0
No don't do that, that's terrible coding and it'll only slow down the rapid showing of GUIs NoahWillCode 370 — 8y
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 mrcool2 75 — 8y
Ad

Answer this question