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
So you need to use debounce
01 | CD.MouseClick:Connect( function (player) |
02 | local healAmount = 15 |
03 | local canDrink = true |
04 | local cooldown = 0.8 |
05 | local h = player.Character.Humanoid |
06 | local currentHealth = h.Health |
07 | local newHealth = currentHealth + 15 |
08 | local gui = player.PlayerGui.Interaction.Int |
09 | local db = false |
10 |
11 | if 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 |
It should work but haven't tested
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:
01 | local healAmount = 15 |
02 | local canDrink = true |
03 | local cooldown = 0.8 |
04 |
05 | CD.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 |
20 | 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.