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 :(
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)