so the script is supposed to give you one gem and activate a function to move the gem to another location.
while wait() do
for i,v in pairs(gems:GetChildren()) do
v.Hitbox.Touched:connect(function(hit)
if debounce == false then
debounce = true
if hit.Parent:FindFirstChild("Humanoid") then
Collected(v)
game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
end
debounce = false
end
end)
end
end
local debounce = false
for i,v in pairs(gems:GetChildren()) do
v.Hitbox.Touched:connect(function(hit)
if debounce then
return
end
debounce = true
if hit.Parent:FindFirstChild("Humanoid") then
Collected(v)
game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
end
debounce = false
end)
end
Your Touched connection is activating thousands of times because you are registering connections every ~0.03 seconds (wait()). What are you trying to achieve?
function addGem(gem)
local debounce = false
gem.Hitbox.Touched:connect(function(hit)
if debounce then
return
end
debounce = true
if hit.Parent:FindFirstChild("Humanoid") then
Collected(gem)
game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
end
debounce = false
end)
end
Where did you define the status of your variable “denounce”? Can you show us your whole script? You’re missing essential info for us to help you out here…
When I was learning about debounces it took me a bit to wrap my head around them but they’re actually very simple.
Basically, you’re only letting something happen when the debounce is false, and so when something happens, you set it to true, so that it can’t happen again. In your script, you’re setting it to true but then almost immediately setting it back to false, letting it happen again. If you only want it to happen once, just delete “debounce = false” at the end, but if you just want a delay, add a wait(time here) right before it to give it some time.
local debounce = false
while wait() do
for i,v in pairs(gems:GetChildren()) do
v.Hitbox.Touched:connect(function(hit)
if debounce == true then
elseif debounce == false then
debounce = true
if hit.Parent:FindFirstChild("Humanoid") then
Collected(v)
game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
wait(1) -- Add a cooldown for the debounce
debounce = false
end
end
end)
end
end
Instead of using a debounce variable why not use the wait() function?
This is what I would do:
while wait() do
for i,v in pairs(gems:GetChildren()) do
v.Hitbox.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
Collected(v)
game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
end
script.Parent.Transparency = 1 --Not required but makes it look nicer.
script.Disabled = true --Don't allow users to use the script
wait(5) --Wait X seconds
script.Parent.Transparency = 0 --Make it seeable again
script.Disabled = false --Re-enable the script.
end
end)
end
end
I can’t test this as I don’t have the collected var but it should work.
debounce=false
cooldown=2
while wait() do
for i,v in pairs(gems:GetChildren()) do
v.Hitbox.Touched:connect(function(hit)
local humanoid=hit.Parent:FindFirstChild("Humanoid")
if debounce == false then
debounce = true
if humanoid then
Collected(v)
game.Players[hit.Parent.Name].leaderstats.Gems.Value += 1
end
wait(2)
debounce = false
else
return
end
end)
end
end
Since for some reason everyone wants to keep the while loop in the code, I’m just gonna mention something that should be kind of obvious.
You. Don’t. Need. The. While. Loop.
A simple script like this…
while wait() do
script.Parent.Touched:Connect(function()
print("LAG INBOUND!")
end)
end
And the massive lag spike that comes just from touching the part should show why this is bad. And guess what, adding a debounce won’t help when you have 600+ touched events connected to your instance just seconds after running your code.
I know this post is kind of getting ranty, but for the past two hours this has just been overlooked. You only need to make an event connection once. And the effects of using this while loop run down into your code. Even with a proper debounce you risk massive performance drops or have the debounce be a global debounce meaning once one player touches any part the debounce is applied to all parts in the folder.
I could not reinforce this enough but get rid of the while loop. Code like shown below is completely functional.
for _,part in ipairs(workspace:WaitForChild("Folder"):GetChildren()) do
part.Touched:Connect(function()
local debounce = false
if not debounce then
print("YAY!")
debounce = not debounce
wait(2)
debounce = not debounce
end
end)
end