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

How to make a script that when you click a button (clickdetector) it gives you more health?

Asked by
jakeovi -1
4 years ago

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:

01local part = script.Parent
02local function Activated(player)
03    script.Parent.ClickDetector.MaxActivationDistance = 0
04    script.Parent.Sound:Play()
05    player.Character.Torso.Anchored = true
06wait (1)
07    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
08wait (1)
09    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
10wait (1)
11    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
12wait (1)
13    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
14wait (1)
15    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
View all 31 lines...

3 answers

Log in to vote
1
Answered by 4 years ago
1player.Character.Humanoid.MaxHealth = newValue
2player.Character.Humanoid.Health = newValue

MaxHealth makes current health stay the same, so you need to increase current health.

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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:

01local part = script.Parent
02local function Activated(player)
03    script.Parent.ClickDetector.MaxActivationDistance = 0
04    script.Parent.Sound:Play()
05    player.Character.Torso.Anchored = true
06for i = 1,10 do
07wait(1)
08    player.Character.Humanoid.Health =
09    player.Character.Humanoid.Health + 375
10end
11player.Character.Torso.Anchored = false
12script.Parent.Sound:Stop()
13    script.Parent.ClickDetector.MaxActivationDistance = 10
14end
15 
16part.ClickDetector.MouseClick:Connect(Activated)

Also remember that the default max health is 100 Used article: Loops

Log in to vote
0
Answered by 4 years ago

This is what I would try :)

1local maxHealth = 100 --Change this to the largest amount of health a player can have.
2local heal = 1 -- Change this to the amount you want one click to heal.
3-- Put the Script inside of ClickDetector
4script.Parent.MouseClick:Connect(function(plr)
5    plr.Character.Humanoid.MaxHealth = maxHealth
6    plr.Character.Humanoid.Health = plr.Character.Humanoid.Health + heal
7end)

Or if you want it to loop and heal 1 every second (That looks like what you're trying to do), try this:

01local maxHealth = 100 --Change this to the largest amount of health a player can have.
02local heal = 1 -- Change this to the amount you want one click to heal.
03local counter = 10 -- How many times you want it to heal with one click
04-- Put the Script inside of ClickDetector
05script.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 
14end)

Answer this question