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

How to make a Player spawn with Max Health over 100?

Asked by 6 years ago
Edited 6 years ago

The game I am currently making has levels that increase health as you level up. How do I make it so that the player spawns with the max health instead of 100? Any help would welcome! Script im adding it too:

game.Players.PlayerAdded:connect(function(Player) 
    local Leaderstats = Instance.new("Folder", Player) 
    Leaderstats.Name = "Leaderstats" 
    local Level = Instance.new("IntValue", Leaderstats) 
    Level.Name = "LevelLable" 
    Level.Value = 1 
    local Exp = Instance.new("IntValue", Leaderstats) 
    Exp.Name = "ExpLable" 
    Exp.Value = 0 
    if Player.Character and Player.Character:FindFirstChild("Humanoid") then
        Player.Character.Humanoid.MaxHealth = 90 + Level.Value*10
    end
    Level.Changed:connect(function(value)
    if Player.Character and Player.Character:FindFirstChild("Humanoid") then
        Player.Character.Humanoid.MaxHealth = 90 + Level.Value*10
        end
    end)
    Player.CharacterAdded:connect(function(character)
        character:WaitForChild("Humanoid").MaxHealth = 90 + Level.Value*10
    end)
end)
0
Edit this post and format your code by clicking on the blue Lua icon which will insert two lines of "~~~~". Place the code in between these two lines. Makes it easier for us to read. xPolarium 1388 — 6y
0
Sorry, my first time using this site! ranger176947 14 — 6y

2 answers

Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years ago

Your mistake was on line 17 and 23 (See below) of forgetting to put in two multiplication symbols: *.

Also use :Connect over lower case :connect because :connect is deprecated. Meaning in the future it can be unsupported which will end up breaking your games/code.

game.Players.PlayerAdded:connect(function(Player) 

    local Leaderstats = Instance.new("Folder") --Don't use the 2nd parameter of Instance.new to set the parent
    Leaderstats.Parent = Player --Better to set it here...
    Leaderstats.Name = "Leaderstats" 
    local Level = Instance.new("IntValue") 
    Level.Parent = Leaderstats --Same here...
    Level.Name = "LevelLable" 
    Level.Value = 1 
    local Exp = Instance.new("IntValue")  
    Exp.Parent = Leaderstats --and same here...
    Exp.Name = "ExpLable" 
    Exp.Value = 0 

    --Remember that in Solo Mode, the character loads in before some scripts. Use a WaitForChild instead.
    if Player.Character and Player.Character:WaitForChild("Humanoid") then 
        Player.Character.Humanoid.MaxHealth = 90 + (Level.Value * 10) 
--Missing " * ". Added Paranthesis to make it cleaner; its math.
    end 

    --Look up on RobloxDev/Wiki for an explanation on deprecation. Use :Connect over lower case :connect from now on.
    Level.Changed:Connect(function(value) 
        if Player.Character and Player.Character:FindFirstChild("Humanoid") then 
            Player.Character.Humanoid.MaxHealth = 90 + (Level.Value * 10) 
            --Missing " * "
        end 
    end) 

    Player.CharacterAdded:Connect(function(character) 
        character:WaitForChild("Humanoid").MaxHealth = 90 + (Level.Value * 10)
    end) 

end)

Edit:

So for setting the Health to full is as simple as setting it to the MaxHealth after MaxHealth has been set to its new value.

Player.Character.Humanoid.MaxHealth = 90 + (Level.Value * 10) 
Player.Character.Humanoid.Health = Player.Character.Humanoid.MaxHealth
--Use this line for the other functions.
0
... I feel really stupid now TBH.... DX... Thanks again lol ranger176947 14 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

To increase a players health on spawn you do this

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:connect(function(Char) --fires when the character is added, this includes when the player dies aswell
        if Char:FindFirstChild("Humanoid") then
            Char.Humanoid.MaxHealth = 200 --Change max health first
            Char.Health = 200 -- then change the health itself
        end
    end)
end)

0
works even if the character dies aswell :D NumbersInBinary 33 — 6y
0
Ok but lets say the at lv 20 you have 200 health... then when you get to level 30 you have 300. How would I change it so that it will make it spawn with the full health even if the health is constantly changing? ranger176947 14 — 6y
0
Updated my answer for this. Use the same line for the other functions. xPolarium 1388 — 6y

Answer this question