Part gives too many leaderstat points

I’m trying to make it so that if you touch a part, it adds to a leaderstat but it constantly gives points while touched instead of after 5s

script.Parent.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		player.leaderstats["Towers Completed"].Value = player.leaderstats["Towers Completed"].Value + 1
		wait (5)
	end
end)

Any help will be greatly appreciated!

Use Debounce:

(The reason it increments the value more than once is because when player touches it is touched 5-10 times, but debounce can add a cooldown for touched event)

local debounce = false
script.Parent.Touched:Connect(function(part)
if part.Parent:FindFirstChild(“Humanoid”) then
if not debounce then
debounce = true
local player = game.Players:GetPlayerFromCharacter(part.Parent)
player.leaderstats[“Towers Completed”].Value = player.leaderstats[“Towers
Completed”].Value + 1
end
wait(5)
debounce = true
end
end)

1 Like

Add a Debounce, which is just a cooldown, all you need to do to accomplish this is to check whether its active, or wait until you deactivate it

local Debounce = false
-- Here is what the Debounce will tell us
-- If True, The Debounce is Currently active
-- If False, we can Activate the Button

-- While inside the Event

if Debounce then return end 
-- this will stop the code below from running if the current conditions are true

task.wait(5) -- 5 seconds to yield the code

Debounce = false -- the code can be fired as the condition above is now false

However, an alternative to check for truthy or falsy values would be to check the elapsed time since the Event occurred, that way we do not have to yield our code, and all we have to do is a simple time check, or this example we will use tick(), to check for the elapsed time, we subtract tick() with the time we have currently stored, which will give us the difference between them, aka our elapsed time.

local LastTouch = tick() -- the time since our last touch

-- During the Event:

local Elapsed = tick() - LastTouch
-- LastTouch will never change while constantly check the current value of tick()
-- will, it will always return a different time, and will always be bigger
-- than LastTouch, making it ideal for checking Elapsed Time

if Elsaped < 5 then return end
-- This will check if our Elapsed Time is less than 5 to stop
-- if the time is greater than 5, we will be able to continue
-- otherwise we will be stopped

-- However if we do not properly manage this, we will be able to fire
-- this forever with no cooldown, so when the code fires after the
-- condition is met, make sure you apply a new time to check for
-- Like so:
LastTouch = tick()
-- which will reset the Debounce

Debounces aren’t that Advanced, but they aren’t that Efficient if you don’t use them properly, The Second Method I provided is likely more Efficient that the first method as you do not yield the code at all, but check how much time as passed.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.