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