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:

local part = script.Parent
local function Activated(player)
    script.Parent.ClickDetector.MaxActivationDistance = 0
    script.Parent.Sound:Play()
    player.Character.Torso.Anchored = true
wait (1)
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
wait (1)
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
wait (1)
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
wait (1)
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
wait (1)
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
wait (1)
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
wait (1)
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
wait (1)
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
wait (1)
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
wait (1)
    player.Character.Humanoid.Health = player.Character.Humanoid.Health + 375
player.Character.Torso.Anchored = false
script.Parent.Sound:Stop()
    script.Parent.ClickDetector.MaxActivationDistance = 10
end

part.ClickDetector.MouseClick:Connect(Activated)

3 answers

Log in to vote
1
Answered by 4 years ago
player.Character.Humanoid.MaxHealth = newValue
player.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:

local part = script.Parent
local function Activated(player)
    script.Parent.ClickDetector.MaxActivationDistance = 0
    script.Parent.Sound:Play()
    player.Character.Torso.Anchored = true
for i = 1,10 do
wait(1)
    player.Character.Humanoid.Health = 
    player.Character.Humanoid.Health + 375
end
player.Character.Torso.Anchored = false
script.Parent.Sound:Stop()
    script.Parent.ClickDetector.MaxActivationDistance = 10
end

part.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 :)

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

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

local maxHealth = 100 --Change this to the largest amount of health a player can have.
local heal = 1 -- Change this to the amount you want one click to heal.
local counter = 10 -- How many times you want it to heal with one click
-- Put the Script inside of ClickDetector
script.Parent.MouseClick:Connect(function(plr)
    plr.Character.Humanoid.MaxHealth = maxHealth
    local count = counter
    while count > 0 do
        plr.Character.Humanoid.Health = plr.Character.Humanoid.Health + heal
        wait(1)
        count = count - 1
    end

end)

Answer this question