Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Why is there no cooldown for my zombie damage script?

Asked by 3 years ago

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)


2 answers

Log in to vote
0
Answered by 3 years ago

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.

Be sure to check out the Roblox API Documentation as well for additional reference.

0
local Debounce = false local WaitTime = 2 function dmg(hit) if hit.Parent ~= nil then if not Debounce then Debounce = true local hum = hit.Parent:findFirstChild("Humanoid") if hum ~= nil then if hit.Parent ~= nil then hum.Health = hum.Health -10 wait(2) Debounce = false end end end end end rarm.Touched:connect(dmg) larm.Touched:connect(dmg) It didn't work :/ BayernLucario25TWO 12 — 3y
0
Did you get an error? TaxesArentAwesome 514 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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

0
It didn't work. The zombie didn't do damage at all. BayernLucario25TWO 12 — 3y
0
there are actually scripts inside of the roblox studio that are better than this one this one doesnt work because you are trying to find to many things. There is a simpler way to do this coolmanHDMI 72 — 3y

Answer this question