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

Ugh! Debounce problems AGAIN! Is this code fixable?

Hello all, me again. Have another debounce issue, and don't understand as to why this isn't working properly! Looking to make a GUI pop up if player doesn't have the proper leaderstat value, or more than the proper leaderstat value!

Here is my SCRIPT and the GUI is the child of the SCRIPT

Any and ALL HELP is appreciated!

local debounce = false

script.Parent.Touched:Connect(function(hit)
	debounce = true
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local lvl = player.leaderstats.Door1
	if player and lvl.Value > 1 and not player.PlayerGui:FindFirstChild('buydoor') then
		script.Parent.CanCollide = false
	else
		local cloneui = script.buydoor:Clone()
		cloneui.Parent = player.PlayerGui
		cloneui.Frame:TweenPosition(UDim2.new(0.25,0,0.2,0))
		debounce = false
	end
end)

3 answers

✓ Accepted answer

Debounces are used to regulate how many times an event can be fired. In your code you are currently not regulating anything, you're just changing a variable.

I modified your script, added comments around the debounce area so you can read those if you want a better understanding of what I changed.

local debounce = false

script.Parent.Touched:Connect(function(hit)
    if debounce == true then return end --//If debounce is true, then return nil
    debounce = true --//Turn the debounce to true to make sure it doesn't fire the event again during the execution of the code below

    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local lvl = player.leaderstats.Door1
    if player and lvl.Value > 1 and not player.PlayerGui:FindFirstChild('buydoor') then
        script.Parent.CanCollide = false
    else
        local cloneui = script.buydoor:Clone()
        cloneui.Parent = player.PlayerGui
        cloneui.Frame:TweenPosition(UDim2.new(0.25,0,0.2,0))
    end

    debounce = false --//We have to put this at the end to make sure the debounce becomes false again and the event is activated once more
end)

3 comments

0

I believe this code will work, but I'm experiencing a problem now as well with the local lvl as it's calling for "player" which is nil to the server. I've added :WaitForChild("leaderstats"):WaitForChild("Door1") which didn't work, and still says it is nil, how to fix this?

0

change the if statement on line 9 to: if player and player.leaderstats.Door1.Value > 1 and not player.PlayerGui:FindFirstChild('buydoor') then

0

Of course if this is a server script like you say, the "buydoor" inside of the PlayerGui must have been put there by the server too. If it wasn't and filtering is enabled you'll probably run into another error.

you didnt add the if statement for the debounce you just had a variable named debounce laying around doing nothing and your just setting it to true and false

From what I've been told, you shouldn't do that. This is what I was told to do:

if not debounce then 
	debounce = true

-- or this:

if debounce = false then
	debounce == true

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