✓ Accepted answer
Debounce is a way to prevent the script from running multiple times. The variable could be any name, and practically have any value. Whether it's a number, string, or boolean, debounce will still work! A majority of scripters like to have it as a variable set within the script, with a boolean, rather than having the object as a descendant. So, let's get started!
on = false
script.Parent.Touched:connect(function(hit)
if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then
if on == false then
on = true
print("I'm hit!")
wait(2)
on = false
end
end
end)
I noticed you were wondering if the variable needed to be local. There are 2 types of variables. Global and local. Local variables' values are only accessible within that function. Global variables are accessible throughout the script. The only time it needs to be local, is if it's originally set/defined within a function. Example:
function Test()
local abc = 123
end
Testing = 515
If you're not originally defining the variable within a function, there's no need for the local tag. Now, with all of that knowledge, apply that to your script!
If you have any questions, feel free to ask in the comments!
4 comments
You should generally use local variable whenever possible, even outside of functions. Not only are they more readable, but they will run faster and more efficiently.
Yes, I've heard that before...
Why would you check if hit has a Parent? That makes absolutely no sense-it will always have a parent.
Wow, looks like we have a hater. I suppose you're right. I just tend to have it there, If I ever have a wait before it. In case it is nil