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

How to make Max Health increase with Levels?

Asked by 8 years ago

Hello!

I am trying to make the Max Health of a player increase as they level up. I've included the entire leaderboard script below, All of it works fine when I remove the Health portion of the script, but I'd really like to get that part functioning too. I've approached it several different ways, generally getting the output that "Health" or "Humanoid" or "plr" is not a valid part of Workspace.

Any advice would be much appreciated!

The problematic part of the script:


local Health = game.Workspace.plr.Health local MaxHealth = game.Workspace.plr.Health * level.Value + 10

The entire leaderboard script:

game.Players.PlayerAdded:connect(function(plr)
    local stats = Instance.new('IntValue', plr)
    stats.Name = 'leaderstats'

local points = Instance.new('IntValue', stats) 
    points.Name = 'Points'
    points.Value = 0
local experience = Instance.new('IntValue', stats)
    experience.Name = 'EXP'
    experience.Value = 0
local level = Instance.new('IntValue', stats)
    level.Name = 'Level'
    level.Value = 0

-- The two lines below is where it breaks
local Health = game.Workspace.plr.Health
local MaxHealth = game.Workspace.plr.Health * level.Value + 10
-- The two lines above is where it breaks   


experience.Changed:connect(function()
    local required=(level.Value*70) -- Number reflects the EXP points needed to level up.
    if experience.Value >= required then
        level.Value=level.Value+1
        experience.Value=experience.Value-required
            end


end)
end)    

3 answers

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago
Edited 8 years ago

The error is because 'plr' is not a member of workspace, it is instead a player object. Also, 'Health' is not a member of plr.

If you want to to implement this you are going to need to get the character of the player and set the health of that character'a humanoid to the desired max health. But theirs another catch, as this value will be reset each time the character respawns. To fix that we can use the CharacterAdded event and set the health each time the character respawns.

We also want to force MaxHealth to update every time the value changes as well. To do that we can use the changed event.

As replacement to your two problematic lines, try something like this:

If plr.Character and player.Character:FindFirstChild("Humanoid") then
    plr.Character.Humanoid.MaxHealth = 100 + level.Value*10
end

level.Changed:connect(function(value)
    if plr.Character and player.Character:FindFirstChild("Humanoid") then
        plr.Character.Humanoid.MaxHealth = 100 + level.Value*10
    end
end)

plr.CharacterAdded:connect(function(character)
    character:WaitForChild("Humanoid").MaxHealth = 100 + level.Value*10
end)
0
Thank you for the assistance!! :D Once I changed "player" to "plr" throughout, it all worked just fine! Thank you! Never2Humble 90 — 8y
0
Whoops, sorry about that! Typed it up quick without testing. You're welcome though! BlackJPI 2658 — 8y
0
No problem at all good sir! You helped me solve a very nagging problem! Much appreciated! :) Never2Humble 90 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Assuming your variables are all defined - we'd connect the setting of the humanoid's health continuously by setting a connection.

level.Changed:connect(function() MaxHealth = game.Workspace.plr.Health  * level.Value * 10 end)

You can then use the variable to set the player's health on another line.

0
The .Changed event will basically call that line to execute very single time the value changes. AbstractCode 16 — 8y
0
Thank you for the assistance! :) Never2Humble 90 — 8y
Log in to vote
0
Answered by 8 years ago

If you want to use the same variables, you would have to replace it with:

local Health = game.plr.localPlayer.Humanoid.Health
local MaxHealth = game.plr.localPlayer.Humanoid.Health * level.Value + 10

I think this should work, since I've only done this for a couple of weeks, but I see many people do this. Try it out!

0
Thank you for the script! I'll test this one too! :) Never2Humble 90 — 8y
0
Oh and for the first line the first word is "If" but it shouldn't be capitalized. marioblast1244 113 — 7y

Answer this question