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

Why the script don't respect the cooldown?

Asked by 6 years ago

I am trying to make a water drink script, it works but doenst respect the cooldown. how i can fix it?

https://pastebin.com/79L0qXsk

0
doenst awesomeipod 607 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

So you need to use debounce

CD.MouseClick:Connect(function(player)
local healAmount = 15
local canDrink = true
local cooldown = 0.8
local h = player.Character.Humanoid
local currentHealth = h.Health
local newHealth = currentHealth + 15
local gui = player.PlayerGui.Interaction.Int
local db = false

if h and canDrink == true  and not db then
    if h.Health > 0 and h.Health < h.MaxHealth then
            canDrink = false
            local currentHealth = h.Health
            local newHealth = currentHealth + healAmount
            h.Health = newHealth
           db = true
 wait(0.8)
            canDrink = true
       db = false
end
end
end)

It should work but haven't tested

Ad
Log in to vote
0
Answered by 6 years ago

That probably wont even work. I would put all the local values outside of the function except the ones that refer to the parameter as so:

local healAmount = 15
local canDrink = true
local cooldown = 0.8

CD.MouseClick:Connect(function(player)
    local h = player.Character.Humanoid
    local currentHealth = h.Health
    local newHealth = currentHealth + healthAmount
    local gui = player.PlayerGui.Interaction.Int
    if h and canDrink == true then
        if h.Health > 0 and h.Health < h.MaxHealth then
            canDrink = false
            local currentHealth = h.Health
            local newHealth = currentHealth + healAmount
            h.Health = newHealth
            wait(cooldown)
            canDrink = true
        end
    end
end)

It wouldnt "respect the cooldown" because everytime you would click it, it would be true because when you clicked it, it would set it to true right away.

Answer this question