Ive got a script so when you touch it, it adds 1 win to the leaderboard, but when i hit it, it adds multiple ones. How would i only make it add 1?
wins = 1 function onTouched(part) local humanoid = part.Parent:findFirstChild("Humanoid") if (humanoid~=nil) then local thisplr = game.Players:findFirstChild(humanoid.Parent.Name) if (thisplr~=nil) then local stats = thisplr:findFirstChild("leaderstats") if (stats~=nil) then local score = stats:findFirstChild("Wins") if (score~=nil) then score.Value = score.Value + wins wait(0.1) script.Parent:Destroy() end end end end end script.Parent.Touched:connect(onTouched)
The problem is that your button does not have a debounce. When a player hits on your button, they are actually touching it multiple times.
Here is your code with cooldown:
wins = 1 local Debounce = false function onTouched(part) local humanoid = part.Parent:findFirstChild("Humanoid") if (humanoid~=nil) then if Debounce then return end Debounce = not Debounce local thisplr = game.Players:findFirstChild(humanoid.Parent.Name) if (thisplr~=nil) then local stats = thisplr:findFirstChild("leaderstats") if (stats~=nil) then local score = stats:findFirstChild("Wins") if (score~=nil) then score.Value = score.Value + wins wait(0.1) script.Parent:Destroy() end end end end end script.Parent.Touched:connect(onTouched)