local enabled = true script.Parent.Touched:Connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid ~= nil and enabled == true then local enabled = false local player = game.Players:GetPlayerFromCharacter(hit.Parent) local points = player.leaderstats.Points points.Value = points.Value + 5 wait(3) enabled = true end end)
So recently I'm learning how to code and I stumbled upon an error while coding a touched function. I tried debugging the error, but couldn't simply fix it. So I basically want to give my player five points each time the player touches an object. However, the script keeps on giving me way more than five points per touch. I added a de-bounce, but the error still occurs. Is there a way I can fix this?
This is because you're not implementing your debounce correctly
Notice how I removed the local from enabled = false. You were creating a new enabled variable inside this event namespace.
local enabled = true script.Parent.Touched:Connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid ~= nil and enabled == true then enabled = false local player = game.Players:GetPlayerFromCharacter(hit.Parent) local points = player.leaderstats.Points points.Value = points.Value + 5 wait(3) enabled = true end end)