I am attempting to create a part that when touched gives players points, which it does, but gives them more than intended and even with a debounce is very glitchy. Also I am trying to make it so everyone who touches it gets only a single coin and win, but the debounce for the first player to touch doesn't allow this. This is what I have so far.
local brick = script.Parent local buttonPressed = false function onTouched(hit) if not buttonPressed then local buttonPressed = true if hit.Parent:FindFirstChild("Humanoid")~=nil and game.Players:GetPlayerFromCharacter(hit.Parent)~=nil then local player=game.Players:GetPlayerFromCharacter(hit.Parent) if player.Character then local stat = player:FindFirstChild("leaderstats") if stat then player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1 player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1 hit.Parent.HumanoidRootPart.CFrame = CFrame.new(-40, 1.5, 64) wait(10) buttonPressed = true end end end end end brick.Touched:connect(onTouched)
The reason your debounce isn't working is because you're using it incorrectly, firstly the parts where you change the debounce should be on the outer skirts of the function, you are also redefining the debounce locally inside the function First let's remove unnecessary if statements.
local brick = script.Parent local buttonPressed = false function onTouched(hit) if buttonPressed == false then buttonPressed = true if hit.Parent:FindFirstChild("Humanoid")~=nil and game.Players:GetPlayerFromCharacter(hit.Parent)~=nil then local player=game.Players:GetPlayerFromCharacter(hit.Parent) if player.Character then local stat = player:FindFirstChild("leaderstats") if stat then player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1 player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1 hit.Parent.HumanoidRootPart.CFrame = CFrame.new(-40, 1.5, 64) wait(10) end end end buttonPressed = false end end brick.Touched:connect(onTouched)
Now the debounce should be working properly
Im going to combine your function and where you call it together now
local brick = script.Parent local buttonPressed = false brick.Touched:connect(function(hit) if buttonPressed == false then buttonPressed = true if hit.Parent:FindFirstChild("Humanoid")~=nil and game.Players:GetPlayerFromCharacter(hit.Parent)~=nil then local player=game.Players:GetPlayerFromCharacter(hit.Parent) if player.Character then local stat = player:FindFirstChild("leaderstats") if stat then player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1 player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1 hit.Parent.HumanoidRootPart.CFrame = CFrame.new(-40, 1.5, 64) wait(10) end end end buttonPressed = false end end)