My goal is to have an initial/max custom player health for my Roblox place in Roblox Studio.
To do this I have created a script in StarterPlayer/StarterPlayerScript
with the following content:
local DEFAULT_HEALTH = 10
game.Players.PlayerAdded:connect(function(player)
player.character.Humanoid.MaxHealth = DEFAULT_HEALTH
player.character.Humanoid.Health = DEFAULT_HEALTH
end)
I have also tried with:
local DEFAULT_HEALTH = 10
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.MaxHealth = DEFAULT_HEALTH
Humanoid.Health = DEFAULT_HEALTH
in both cases when I playtest it (TEST->PLAY
) it seems the default health=100 is used instead of health=10
Is it possible to fix the issue outlined?
The PlayerAdded event fires as soon as the player joins the server, but their character model doesn't load into the world immediately. So you need to wait for the player's character model to spawb. Luckily, you can use the CharacterAdded event to know when that happens.
So using a Script in the Workspace or ServerScriptService, try this :
local DEFAULT_HEALTH = 10
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:Connect(function(character)
character.Humanoid.MaxHealth = DEFAULT_HEALTH
character.Humanoid.Health = DEFAULT_HEALTH
end)
end)