Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my cooldown script not working?

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

✓ Accepted answer

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.

2 comments

0

Great! Thanks :)

0

No problem, I like how duckwit edited his answer after noticing that I wasn't using local before the variable.

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!

2 comments

0

Awesome! Thanks :)

0

Remember to mark a functional answer as the accepted one when you're done with a thread ;)

This discussion is preserved as it was. New answers, comments, and votes are closed.