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

Explain Debounce?! Help!

Asked by 9 years ago

So I've been using debounce for a long time now and i had many problems. For example, why when i make debounce for a point giver brick onTouch and it activates debounce for all players not just the player who touched it. So when 1 person touches it it gives him points, but when another person touches it right after that person it doesn't do anything. Same thing when i used making GUI slide on screen on brick onTouch, it works for one person, but right after that when another person touches it it doesn't work for them, unless they wait 10 seconds, then it works for the first person to touch it again. Why does it set debounce for everyone and not just the person who touched the brick? Please explain?

How would i use debounce?

debounce = false

script.Parent.Touched:connect(function(hit)
if debounce == false then
debounce = true
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.PlayerGui.Intro.Window.Text.Visible = true
player.PlayerGui.Intro.Window:TweenPosition(UDim2.new(0,0,0.5,150))
player.PlayerGui.Intro.Window.Text.Text = "Remember what i said? Red bricks are a no no. Try to avoid them as much as possible! (Hint: They kill you) ~NonSpace"
-- first time learning Tweening!
wait(10)
player.PlayerGui.Intro.Window:TweenPosition(UDim2.new(0,0,0.5,400))
wait(1)
player.PlayerGui.Intro.Window.Text.Visible = false

debounce = false
end
end
end)

-- Only works for the first person :(
0
Please don't thumbs down, i just need to know. Its urgent NonSpace 0 — 9y

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

What you are doing is setting the debounce for the function, not the player. Whenever your function is run by any player, the debounce will change value and the function will not be able to fire again until the debounce's value is changed back again.

If you want to make a debounce for each individual player, you could either use a local script for every player or do checks on each player that touches it to see if enough time has elapsed. The local script way is the easiest of the two.

Local Script Example:

-- This should be in StarterGui or StarterPack

local debounce = false

function doSomething()
    if debounce = false then
        debounce = true
        print("Did Something")
        wait(1)
        debounce = false
    else
        print("Did Nothing")
    end
end

doSomething()

Single Script Example:

local players = {}

game.Players.PlayerAdded:connect(function(player)
    players[player] = false -- Add a player to the list
end)

game.Players.PlayerRemoving:connect(function(player)
    for index, _ in ipairs (players) do -- Search for player
        if index == player then
            table.remove(players, index) -- Remove player from list
        end
    end
end)

function doSomething(playerWhoDidSomething)
    if players[playerWhoDidSomething] == false then
        players[playerWhoDidSomething] = true
        -- code
        players[playerWhoDidSomething] = false
    end
end

doSomething()

On Touched Example:

local debounce = false
local part = game.Workspace.PartToTouch
local waitTime = 2

part.Touched:connect(function()
    if debounce == false then
        debounce = true
        -- code
        wait(waitTime)
        debounce = false
    end
end)
0
That makes sense i guess, but how do i do the same thing for a brick onTouch, and don't you have to set debounce to true? NonSpace 0 — 9y
0
The debounce still works for all players not the individual :( NonSpace 0 — 9y
0
Yeah you do, my bad! I added an example for the single script method and how you would layout a touched function. You can use either method to implement the touched event into. BlackJPI 2658 — 9y
0
follow me so i can show you what i mean NonSpace 0 — 9y
0
I tried changing it to Local Script and pasting it in StarterGui, and changed script.Parent to game.Workspace.Hint1, but it still works the same and the debounce activates for the whole server :( NonSpace 0 — 9y
Ad

Answer this question