Hello! i need help with adding a debounce to this server script:
so basically i want it to be toucheable by every player, but when it is touched by a player, the player that touched cannot touch it anymore, but others can if they havent touched yet.
local button = script.Parent
button.Touched:Connect(function(hit)
if hit.Parent:findFirstChild("Humanoid") ~= nil then
warn("button touched by "..hit.Parent.Name)
end
end)
here’s what it prints:button touched by kaixiuuu (x16)
here’s what i tried:
local button = script.Parent
local debounce = false
button.Touched:Connect(function(hit)
if hit.Parent:findFirstChild("Humanoid") ~= nil and not debounce then
debounce = true
warn("button touched by "..hit.Parent.Name)
debounce = false
end
end)
here’s what it prints:button touched by kaixiuuu (x13)
Its kinda obvious for now since the debounce is set to false back but if i remove the debounce = false, nobody else wont be able to touch it, so how do i solve this?
local button = script.Parent
local Debounce = {}
local Cooldown = 3 --seconds
button.Touched:Connect(function(hit)
if hit.Parent:findFirstChild("Humanoid") ~= nil then
if not table.find(Debounce, hit.Parent.Name) then
warn("button touched by "..hit.Parent.Name)
spawn(function()
table.insert(Debounce, hit.Parent.Name)
wait(cooldown)
table.remove(Debounce, hit.Parent.Name)
end)
end
end
end)
If you want it slightly different, the way that you would remove the user from the table is table.remove(Debounce, hit.Parent.Name),and the way you add them to the table is table.insert(Debounce, hit.Parent.Name)
This code is either outdated or flawed, but considering this post was 9 months ago and left unchecked, it’s extremely risky to entrust your game with either way; it spews multiple errors whenever it is touched.
The reason why this code spews errors is due to the fact that it is trying to delete a string instead of setting it to nil and failing in the process.
local Debounce = {}
brick.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") ~= nil then
if not table.find(Debounce, hit.Parent.Name) then
--Inserts name value
warn("brick was touched by "..hit.Parent.Name)
table.insert(Debounce, hit.Parent.Name)
--Sets name value to nil
print("Inserted: ", Debounce)
Debounce[hit.Parent.Name] = nil
--Prints
warn("deleted "..hit.Parent.Name.." from debounce table")
print("Removed: ", Debounce)
--You can delete this if you want; it's just a technical check
if Debounce[hit.Parent.Name] then
error("There still exists "..hit.Parent.Name.." in the Debounce table")
else
print("All is successful, no errors!")
end
end
end
end)