I have been trying to implement while loops but nothing seems to work. Keep in mind I am no programmer, my friend made this code and I am just trying to help him. Please keep your explanations simple if possible. Here is the code:
local rarm = script.Parent:FindFirstChild("RightUpperArm") local larm = script.Parent:FindFirstChild("LeftUpperArm") function dmg(hit) if hit.Parent ~= nil then local hum = hit.Parent:findFirstChild("Humanoid") if hum ~= nil then if hit.Parent ~= nil then hum.Health = hum.Health -10 wait(2) end end end end rarm.Touched:connect(dmg) larm.Touched:connect(dmg)
Howdy!
In the scripting world, we call this "debounce". Don't ask me why, the Code Gods just made it like that. The definition used by this site is:
Debounce is a term that is often associated with the act of adding a cool-down time to a function so it doesn't get called rapidly. This can be especially helpful when used in conjunction with the Touched event of parts. Oftentimes, this is accomplished by setting a variable to true at the beginning of a function, and setting it to false again at the end. Then, at the very beginning of the function, if the variable is already true, then you use the 'return' keyword, effectively ending the function.
Before your function, add a variable called "debounce" and set it to false. Then when the function is ran, set the debounce as true with a wait time of three seconds (or whatever you want). Take a look at the example I have below where I have a script set to print something upon the click from player on a ClickDetector.
local Debounce = false local WaitTime = 3 -- The amount of time you want to wait script.Parent.MouseClick:Connect(function(Player) if not Debounce then Debounce = true print("I just got clicked by ".. Player.Name .."!" wait(WaitTime) Debounce = false end end)
If this helped you out, consider accepting this answer for those sweet, sweet reputation points. If not, comment below and I (or someone else) will help you out.
Here is a simple way to add cooldowns to scripts. You can use something called debounce or db. You can set it to true or false by default and it needs to be set to the inverse of its original setting as soon as the script starts. So like this
local rarm = script.Parent:FindFirstChild("RightUpperArm") local larm = script.Parent:FindFirstChild("LeftUpperArm") local debounce = true function dmg(hit) if hit.Parent ~= nil and debounce == true then debounce = false local hum = hit.Parent:findFirstChildOfClass("Humanoid") if (hum ~= nil) then if hit.Parent ~= nil then hum.Health = hum.Health -10 wait(2) debounce = true end end end end rarm.Touched:connect(dmg) larm.Touched:connect(dmg)
This should work