Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

2 comments

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.

0

Sorry, my first time using this site!

2 answers

✓ Accepted answer

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.

1 comment

0

... I feel really stupid now TBH.... DX... Thanks again lol

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)
	

3 comments

0

works even if the character dies aswell :D

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?

0

Updated my answer for this. Use the same line for the other functions.

This discussion is preserved as it was. New answers, comments, and votes are closed.