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:

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

The entire leaderboard script:

01game.Players.PlayerAdded:connect(function(plr)
02    local stats = Instance.new('IntValue', plr)
03    stats.Name = 'leaderstats'
04 
05local points = Instance.new('IntValue', stats)
06    points.Name = 'Points'
07    points.Value = 0
08local experience = Instance.new('IntValue', stats)
09    experience.Name = 'EXP'
10    experience.Value = 0
11local level = Instance.new('IntValue', stats)
12    level.Name = 'Level'
13    level.Value = 0
14 
15-- The two lines below is where it breaks
View all 30 lines...

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:

01If plr.Character and player.Character:FindFirstChild("Humanoid") then
02    plr.Character.Humanoid.MaxHealth = 100 + level.Value*10
03end
04 
05level.Changed:connect(function(value)
06    if plr.Character and player.Character:FindFirstChild("Humanoid") then
07        plr.Character.Humanoid.MaxHealth = 100 + level.Value*10
08    end
09end)
10 
11plr.CharacterAdded:connect(function(character)
12    character:WaitForChild("Humanoid").MaxHealth = 100 + level.Value*10
13end)
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.

1level.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:

1local Health = game.plr.localPlayer.Humanoid.Health
2local 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 — 8y

Answer this question