Making a Counter that changes if someone touches

Alright, so im making a shame corner (dont ask me why)
That if someone touchs the invisible box the counter changes i already did the script for it
script.Parent.Touched:Connect(function()
game.Workspace.Ultimateshamecorner.ASHAMEDCOUNTER.SurfaceGui.Counter.Text = “ASHAMED PEOPLE: 1”
end)
But i want it to repeat like if another player touches the invisble box it changes to 2
Is there anyway to do it?

You could store the number into a variable and then increment the variable by 1 every time someone enters it

local numOfAshamed = 0
script.Parent.Touched:Connect(function()
    numOfAshamed = numOfAshamed + 1
    game.Workspace.Workspace.Ultimateshamecorner.ASHAMEDCOUNTER.SurfaceGui.Counter.Text = "ASHAMED PEOPLE: ".. numOfAshamed
end)
1 Like

For some reason it increases to 7 when i go in

You need to add debounce, because the event is being fired multiple times, thus increasing it multiple times.

From there, debounce is your best friend.

1 Like

If debounce were added to the function @downcrusher69 sent, it would look something like this:

local numOfAshamed = 0 
local debounce = false
script.Parent.Touched:Connect(function() 
    if debounce then return end
    debounce = true
    numOfAshamed = numOfAshamed + 1 
    game.Workspace.Ultimateshamecorner.ASHAMEDCOUNTER.SurfaceGui.Counter.Text = "ASHAMED PEOPLE: ".. numOfAshamed 
    debounce = false
end)
1 Like