I have this script to change a value everytime it’s clicked to true or false, i’m not very much into debounce stuff so i’m pretty sure that’s what’s wrong, can anyone help me fix it please?
Note: cd is a ClickDetector
local value = game.ReplicatedStorage:WaitForChild("Values").Coolant1
local cd = script.Parent
local debounce = true
cd.MouseClick:Connect(function()
if debounce == true then
value.Value = true
debounce = false
end
if debounce == false then
value.Value = false
debounce = true
end
end)
You need to place debounce at the bottom of the function. What is currently happening is debounce is seen as true, so you click the ClickDetector and the first if statement is executed and debounce is changed to false. The second if statement is then executed and is true because you just changed debounce to false. You should also check if debounce is already false at the top of the function. Typically you’ll only be using debounce if a time factor is involved though. This is what your code should look like after the fix.
local value = game.ReplicatedStorage:WaitForChild("Values").Coolant1
local cd = script.Parent
local debounce = false
cd.MouseClick:Connect(function()
if debounce then
return
end
debounce = true
value.Value = not value.Value
wait(1)
debounce = false
end)