Player dies when loading in

Hey I need desperate help.

I am making a script for a game and whenever you load in you just die, how do I prevent that from happeneing?

Script:

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		local maxchi = player:WaitForChild("Stats"):WaitForChild("MaxChi")
		local healthmultiplier = player:WaitForChild("Upgrades"):WaitForChild("HealthMultiplier")

		hum.MaxHealth = (100 + maxchi.Value / 200) * healthmultiplier.Value
		hum.Health = (100 + maxchi.Value / 200) * healthmultiplier.Value
		
		maxchi.Changed:Connect(function()
			hum.MaxHealth = (100 + maxchi.Value / 200) * healthmultiplier.Value
		end)
	end)
end)

Help would mean a lot.

You could put in a print statement and get the values of maxchi and healthmultiplier before you compute hum.MaxHealth and hum.Health. Maybe they aren’t being set correctly and it sets your players health to 0.

It sounds like the issue may be related to the player’s health being set to zero upon loading into the game. One possible solution to prevent the player from dying upon loading in is to delay the execution of the player’s health and max health setter code by a short amount of time after the player spawns in the game. You can do this using a Wait() function.

game:GetService("Players").PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        -- Wait briefly to make sure the character is fully loaded
        wait(1)
        
        local hum = char:WaitForChild("Humanoid")
        local maxchi = player:WaitForChild("Stats"):WaitForChild("MaxChi")
        local healthmultiplier = player:WaitForChild("Upgrades"):WaitForChild("HealthMultiplier")

        hum.MaxHealth = (100 + maxchi.Value / 200) * healthmultiplier.Value
        hum.Health = (100 + maxchi.Value / 200) * healthmultiplier.Value

        maxchi.Changed:Connect(function()
            hum.MaxHealth = (100 + maxchi.Value / 200) * healthmultiplier.Value
        end)
    end)
end)

wait is actually deprecated but still there for backward compatibility so you should be using task.wait instead. An arbitrary time to wait could still causes issues, you might consider this line of code instead:

repeat task.wait() until char.Parent

Have you printed both the maxchi and healthmultiplier?

wait() is actually semi-deprecated for as you said, backwards compatibility (and also for when you want wait with trhottling)