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!
01 | local debounce = false |
02 |
03 | script.Parent.Touched:Connect( function (hit) |
04 | debounce = true |
05 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
06 | local lvl = player.leaderstats.Door 1 |
07 | if player and lvl.Value > 1 and not player.PlayerGui:FindFirstChild( 'buydoor' ) then |
08 | script.Parent.CanCollide = false |
09 | else |
10 | local cloneui = script.buydoor:Clone() |
11 | cloneui.Parent = player.PlayerGui |
12 | cloneui.Frame:TweenPosition(UDim 2. new( 0.25 , 0 , 0.2 , 0 )) |
13 | debounce = false |
14 | end |
15 | end ) |
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.
01 | local debounce = false |
02 |
03 | script.Parent.Touched:Connect( function (hit) |
04 | if debounce = = true then return end --//If debounce is true, then return nil |
05 | debounce = true --//Turn the debounce to true to make sure it doesn't fire the event again during the execution of the code below |
06 |
07 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
08 | local lvl = player.leaderstats.Door 1 |
09 | if player and lvl.Value > 1 and not player.PlayerGui:FindFirstChild( 'buydoor' ) then |
10 | script.Parent.CanCollide = false |
11 | else |
12 | local cloneui = script.buydoor:Clone() |
13 | cloneui.Parent = player.PlayerGui |
14 | cloneui.Frame:TweenPosition(UDim 2. new( 0.25 , 0 , 0.2 , 0 )) |
15 | end |
16 |
17 | 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 |
18 | end ) |
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:
1 | if not debounce then |
2 | debounce = true |
3 |
4 | -- or this: |
5 |
6 | if debounce = false then |
7 | debounce = = true |