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 7 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 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

So you need to use debounce

01CD.MouseClick:Connect(function(player)
02local healAmount = 15
03local canDrink = true
04local cooldown = 0.8
05local h = player.Character.Humanoid
06local currentHealth = h.Health
07local newHealth = currentHealth + 15
08local gui = player.PlayerGui.Interaction.Int
09local db = false
10 
11if h and canDrink == true  and not db then
12    if h.Health > 0 and h.Health < h.MaxHealth then
13            canDrink = false
14            local currentHealth = h.Health
15            local newHealth = currentHealth + healAmount
View all 23 lines...

It should work but haven't tested

Ad
Log in to vote
0
Answered by 7 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:

01local healAmount = 15
02local canDrink = true
03local cooldown = 0.8
04 
05CD.MouseClick:Connect(function(player)
06    local h = player.Character.Humanoid
07    local currentHealth = h.Health
08    local newHealth = currentHealth + healthAmount
09    local gui = player.PlayerGui.Interaction.Int
10    if h and canDrink == true then
11        if h.Health > 0 and h.Health < h.MaxHealth then
12            canDrink = false
13            local currentHealth = h.Health
14            local newHealth = currentHealth + healAmount
15            h.Health = newHealth
16            wait(cooldown)
17            canDrink = true
18        end
19    end
20end)

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