Hello. I made a block which when you touch it it will award you points. It all works fine, but the event is firing multiple times before the debounce is set to false. I’ve tried many ways to prevent it, but none of them fully helped to prevent it.
Here’s my code:
local part = script.Parent
local debounce = false
function on_Hit(hit)
if not debounce then
if hit.Name == "LowerTorso" then
debounce = true
local succed, failed = pcall(function()
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
local leaderstats = player.leaderstats
local pointsValue = leaderstats.Points.Value
local pointsAdd = 1
local result = coinsValue + tonumber(pointsAdd)
leaderstats.Points.Value = result
local model = script.Parent
model:Destroy()
local waitTime = 0.6
if hit then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.PlayerGui.PointEarned.Enabled = true
wait(tonumber(waitTime))
player.PlayerGui.PointEarned.Enabled = false
end
end)
if succed then
warn("Code ran succesfully without any error..")
else
warn("Code failed to run; error: ".. tostring(failed))
end
end
end
end
part.Touched:Connect(on_Hit)
The player is being awarded points multiple times before the debounce is set to true. What can I do to prevent it?