So I'm making a game with a lot of combat and I'm trying to make a button heal. I've tested my code and it didn't work. Is there a way to increase player's health? Here is my code:
01 | local part = script.Parent |
02 | local function Activated(player) |
03 | script.Parent.ClickDetector.MaxActivationDistance = 0 |
04 | script.Parent.Sound:Play() |
05 | player.Character.Torso.Anchored = true |
06 | wait ( 1 ) |
07 | player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375 |
08 | wait ( 1 ) |
09 | player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375 |
10 | wait ( 1 ) |
11 | player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375 |
12 | wait ( 1 ) |
13 | player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375 |
14 | wait ( 1 ) |
15 | player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375 |
1 | player.Character.Humanoid.MaxHealth = newValue |
2 | player.Character.Humanoid.Health = newValue |
MaxHealth makes current health stay the same, so you need to increase current health.
There is a space between wait and (1) (so it is wait (1)). You will need to delete the space (so it is "wait(1)"). Your code is great, but there is a way of making it shorter by using for - do loops:
01 | local part = script.Parent |
02 | local function Activated(player) |
03 | script.Parent.ClickDetector.MaxActivationDistance = 0 |
04 | script.Parent.Sound:Play() |
05 | player.Character.Torso.Anchored = true |
06 | for i = 1 , 10 do |
07 | wait( 1 ) |
08 | player.Character.Humanoid.Health = |
09 | player.Character.Humanoid.Health + 375 |
10 | end |
11 | player.Character.Torso.Anchored = false |
12 | script.Parent.Sound:Stop() |
13 | script.Parent.ClickDetector.MaxActivationDistance = 10 |
14 | end |
15 |
16 | part.ClickDetector.MouseClick:Connect(Activated) |
Also remember that the default max health is 100 Used article: Loops
This is what I would try :)
1 | local maxHealth = 100 --Change this to the largest amount of health a player can have. |
2 | local heal = 1 -- Change this to the amount you want one click to heal. |
3 | -- Put the Script inside of ClickDetector |
4 | script.Parent.MouseClick:Connect( function (plr) |
5 | plr.Character.Humanoid.MaxHealth = maxHealth |
6 | plr.Character.Humanoid.Health = plr.Character.Humanoid.Health + heal |
7 | end ) |
Or if you want it to loop and heal 1 every second (That looks like what you're trying to do), try this:
01 | local maxHealth = 100 --Change this to the largest amount of health a player can have. |
02 | local heal = 1 -- Change this to the amount you want one click to heal. |
03 | local counter = 10 -- How many times you want it to heal with one click |
04 | -- Put the Script inside of ClickDetector |
05 | script.Parent.MouseClick:Connect( function (plr) |
06 | plr.Character.Humanoid.MaxHealth = maxHealth |
07 | local count = counter |
08 | while count > 0 do |
09 | plr.Character.Humanoid.Health = plr.Character.Humanoid.Health + heal |
10 | wait( 1 ) |
11 | count = count - 1 |
12 | end |
13 |
14 | end ) |