Whenever the player respawns, their health doesn't become their MaxHealth

Hello there. I am making a +1 health every second game, and I seem to have found a problem.

Every second, the player’s MaxHealth becomes equal to a IntValue that is located inside the player’s leaderstats. The IntValue is rising by +1 every second.

For some reason, whenever the player joins or respawns, their health becomes 100 instead of the MaxHealth. Sometimes, because of the vast difference of the health and the MaxHealth, the player dies.

I assume this happens because roblox may instantly set the player’s health to 100.

How could I fix this?

2 Likes

You want to set the health and maxhealth at the same time

Humanoid.MaxHealth = IntValue.Value 
Humanoid.Health = Humanoid.MaxHealth

So, where ever you are setting the MaxHealth at when they respawn or rejoin. Make sure their health right after is set to maxHealth.

Any further help, would need a script to see what is going on.

Well, this is my script:

game.Players.PlayerAdded:Connect(function(player)
	while true do
		task.wait(1)
		
		
		local plaour = game.Players[player.Name]
		local char = plaour.Character
		char:WaitForChild("Humanoid").MaxHealth = player:WaitForChild("leaderstats"):WaitForChild("HealthValue").Value
			char.Humanoid.Health += player.leaderstats.Wins.Value
	end
end)

My technique to get the player’s character is a bit atrocious, so it’d be nice if you can give me another way to do that.

Anyways, I don’t really want to make it so that the health becomes the MaxHealth every second, because then, that would make all the damage bricks and obstacles pointless.

an easier way to get the character is just this

Character = player.CharacterAdded:Wait()

Try to not have a while loop either

game.Players.PlayerAdded:Connect(function(player)	

		local Character = player.CharacterAdded:Wait()
        local NewHealth = player:WaitForChild("leaderstats"):WaitForChild("HealthValue")
task.wait(0.5)
Character.Humanoid.MaxHealth = NewHealth.Value
task.wait(0.5)
Character.Humanoid.Health =  NewHealth.Value

NewHealth.Changed:Connect(function(Change)
Character.Humanoid.MaxHealth = player:WaitForChild("leaderstats"):WaitForChild("HealthValue").Value

end)


end)

For some reason, this just didn’t work. I have no idea why.

– Deleted
Try replacing it with this then

Unfortunately, it didn’t work.

I edited the script above, make sure you have the output open up in studio, so you can see if there are any errors.

It didn’t work, and there was nothing in the output.

Remember to read all the notes. Also added your 1 health per second loop.

local Default_Health = 110 -- Sets the default value of characters MaxHealth
-- Connect up a function, that runs when a player joins the game
game.Players.PlayerAdded:Connect(function(Player)
	-- Create leaderstats
	local leaderstats = Instance.new("leaderstats")
	local V_Health = Instance.new("IntValue")
	V_Health.Name = "MaxHealthValue"
	V_Health.Value = Default_Health
	V_Health.Parent = leaderstats
	leaderstats.Parent = Player -- Always parent the leaderstats folder itself lastly
	-- Connect up a function, that runs everytime our V_Health changes value
	V_Health:GetPropertyChangedSignal("Value"):Connect(function()
		local Character = Player.Character
		-- If player has a character currently present
		if Character then
			local Humanoid = Character:FindFirstChildOfClass("Humanoid")
			-- If character has a humanoid currently present
			if Humanoid then
				-- Change humanoid MaxHealth to the value of V_Health
				Humanoid.MaxHealth = V_Health.Value
				Humanoid.Health = V_Health.Value
			end
		end
	end)
	-- Connect up a function, that runs everytime the player character loads
	Player.CharacterAdded:Connect(function(Character)
		-- Wait for humanoid to be present
		local Humanoid = Character:WaitForChild("Humanoid")
		-- Change humanoid MaxHealth to the value of V_Health
		Humanoid.MaxHealth = V_Health.Value
		Humanoid.Health = V_Health.Value
	end)
	-- Load their character after we've setup all the functions
	-- Make sure to disable "CharacterAutoLoads" in the "Players" tab in the Explorer
	Player:LoadCharacter()
end)

-- We spawn a new "thread" here for the while loop to run on, so it does not yield any possible scripting below this loop, if we later on add any.
spawn(function()
	-- We create a endless loop. When it have reached the end of the loop, it waits 1 second before doing it all again
	while task.wait(1) do
		-- We create a loop that loops through all players
		for _, Player in pairs(game.Players:GetPlayers()) do
			local leaderstats = Player:FindFirstChild("leaderstats")
			-- If leaderstats was not present at the time of checking the player, we continue to next player in the loop
			if not leaderstats then continue end
			local V_Health = leaderstats:FindFirstChild("MaxHealthValue")
			-- If V_Health was not present at the time of checking the leaderstats, we continue to next player in the loop
			if not V_Health then continue end
			-- We add 1 to their V_Health value
			V_Health.Value += 1
		end
		-- Reached the end, now waits 1 second before doing it all again
	end
end)

Unfortunately, this didn’t work. What I was trying to achieve was making the player’s health become the player’s MaxHealth or the HealthValue in leaderstats once they join.

But this just repeated what my own script did, and I’m honestly losing hope on if this is even possible.

Image of my health bar from when I first join:
Screen Shot 2022-12-15 at 5.28.29 PM
(Ignore the things that say 74)

Are you trying to save their maxhealth value when they leave and rejoin?

I have a datastore2 thingy I made to save the HealthValue thing that I didn’t show in the script. I didn’t really think it was relevant.

Also I don’t think we really need to worry about that right now, the main problem at hand is that, whenever the player joins or their character is added, their health instantly goes to 100 instead of going to the MaxHealth.

It is quite relevant. Have you already done, so V_MaxHealth’s value is equal to your HealthValue in your datastore on playeradded?

Also are you increasing the HealthValue in the datastore, instead of the V_MaxHealth’s value directly?

Yes, I have done these things.

What I’m most confused about is the fact that the player’s Health instantly lowers to 100. Not MaxHealth, btw.

The reason for the player’s health is set to 100, is that when a character spawns in, the default MaxHealth is 100. When you change that MaxHealth to lets say 200, you also have to set their Health to MaxHealth afterwards.

Added this little piece of code and I want to facepalm right now:

Humanoid.Health = V_Health.Value

I can’t believe I spent like 2 days trying to fix this issue that seemingly couldn’t be fixed.
It’s odd because I’m pretty sure I tried this strategy and was confused why it didn’t work.
Thanks btw.

If that was the solution, please mark it so.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.