Health Not Changing

Hello, developers!

I’m trying to set the player’s Health to their MaxHealth upon respawn. For some reason, it prints that the value of Health is 200 (the desired value) but it is still set to 100 on the client and the server.

My Code:

game:GetService("Players").PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			wait(5)
			character.Humanoid.Health = character.Humanoid.MaxHealth
			print(character.Humanoid.Health)
			print(character.Humanoid.MaxHealth)
			
-- THE FOLLOWING PART HAS TO DO WITH OTHER SYSTEMS AND IS UNRELATED TO THIS
			if player.Team == game.Teams.Runners and player:WaitForChild("Lives").Value ~= 4 then
				player:WaitForChild("Lives").Value -= 1
				
				if player.Lives.Value == 0 then
					wait(1)
					player.PlayerGui.RunnerGui.Died.Visible = true
					player.Character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation1.CFrame + Vector3.new(0, 10, 0)
					task.wait(16)
					if player.Lives.Value == 0 then
						player.PlayerGui.RunnerGui.Died.Visible = false
						player.TeamColor = game.Teams:FindFirstChild("Lobby").TeamColor
						game.Players[game.ReplicatedStorage.Values.Tagger.Value]:WaitForChild("leaderstats").Coins.Value += 25
					end
				
				end
			end
		end)
	end)
end)
2 Likes

Very likely there is a race condition somewhere. Is there any other codes that assigns values to humanoid health?

Try this code.
Your code wasn’t working because humanoid.Died event fires when player dies so your code was setting the dead humanoid health and after respawn new character had default 100 max health. Use player.CharacterAdded it fires when you respawn so new humanoid has max health set to 200.
I don’t know how this script will impact on rest of your code that is in humanoid.died function.

game:GetService("Players").PlayerAdded:connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.MaxHealth = 200
		character.Humanoid.Health = character.Humanoid.MaxHealth
		print("Health: "..(character.Humanoid.Health))
		print("MaxHealth: "..(character.Humanoid.MaxHealth))	
	end)
end)	
1 Like

Thank you, this worked!

charslimit

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