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

Why is my cooldown script not working?

Asked by 10 years ago

I'm quite new to Lua, and I've made a script that heals the player when they step on a block. The healing works but when I try to add a cooldown to it, it doesn't work. This is the script:

local brick = script.Parent
local cd = false

function onTouched(hit)
    while cd = false do
        local cd = true
        brick.BrickColor = BrickColor.Red()
        local hpRestored = 100 - hit.Parent.Humanoid.Health
        for i = 1, hpRestored do
            hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health + 1
            wait(0.03)
        end
        wait(8)
        brick.BrickColor = BrickColor.Green()
        local cd = false
    end 
end

script.Parent.Touched:connect(onTouched)

Thanks :)

2 answers

Log in to vote
1
Answered by 10 years ago

The cooldown wasn't working because you set "local cd = false" when the "local" meant it was creating a new value for cd within the current environment, not changing the actual cooldown. I improved it a bit also as you tried to execute it with a while loop which was not needed as that is for repeating things, not checking. Instead you should check it with an if statement, something like this should work:

local brick = script.Parent
local cd = false

function onTouched(hit)
    if cd == false then --Checking if it is cooling down with an if statement and equals to.
        cd = true --Don't use "local" in front as that would create a new value
        ypcall(function() --Protected it in case there is no humanoid or brick.
            brick.BrickColor = BrickColor.Red()
            local hpRestored = 100 - hit.Parent.Humanoid.Health
            for i = 1, hpRestored do
                hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health + 1
                wait(0.03)
            end
            wait(8)
            brick.BrickColor = BrickColor.Green()
        end)
        cd = false --Once again no "local"
    end
end

script.Parent.Touched:connect(onTouched)

This should work now.

0
Great! Thanks :) Krypticon 20 — 10y
0
No problem, I like how duckwit edited his answer after noticing that I wasn't using local before the variable. Archonious2 160 — 10y
Ad
Log in to vote
0
Answered by
duckwit 1404 Moderation Voter
10 years ago

The while loop is just going to keep churning through for the first person to activate the function, think about it: Every time it gets to the end of the loop you have cd = false, the loop condition is then fulfilled and it'll go straight back to going through the healing process again.

I think you'll want to use an 'if' statement instead - to check whether the function can be activated once. (i.e. when a player touches it, you don't want to keep checking after they've touched it)

Also, you'll want to check that the thing that touched this healing device is a descendent of a Player's Character.

local brick = script.Parent
local cd = false

function onTouched(hit)
    humanoid = hit.Parent:FindFirstChild("Humanoid")
    --Use '==' for comparison, '=' on its own represents an assignment
    if cd == false and humanoid  do --Check that the humanoid exists
        cd = true --No local prefix here either...
        brick.BrickColor = BrickColor.Red()

        local hpRestored = 100 - humanoid.Health
        for i = 1, hpRestored do
            humanoid.Health = humanoid.Health + 1
            wait(0.03)
        end

        wait(8)
        brick.BrickColor = BrickColor.Green()
        cd = false --Also, don't use the "local" prefix here, that makes it a new variable
    end 
end

script.Parent.Touched:connect(onTouched)

Hope that helps!

0
Awesome! Thanks :) Krypticon 20 — 10y
0
Remember to mark a functional answer as the accepted one when you're done with a thread ;) duckwit 1404 — 10y

Answer this question