I’m trying to have a script that makes the players health a leaderstat
You’re waiting for a player to join the game, then waiting for a player to join the game again as a consequence. If this is a Script in ServerScriptService, you’re looking for the following code:
local Players = game:GetService("Players")
local function onPlayerAdded(player: Player)
local leaderstats = player:WaitForChild("leaderstats")
local health = leaderstats:WaitForChild("Health")
local function onHealthChanged(newHealth: number)
local character = player.Character
if character then
character.Humanoid.Health = newHealth
end
end
onHealthChanged(health.Value)
health.Changed:Connect(onHealthChanged)
end
for _, player in Players:GetPlayers() do
task.defer(onPlayerAdded, player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Explanation:
local player = game:GetService("Players").PlayerAdded:Wait()
On this line you are getting the Players service and waiting for its PlayerAdded event. This will most of the time just result in the player variable being the first player that joined your game. I assume you want to have a leaderstat for every single person joining the game, so this is incorrect. Instead, just get the Players service, like this:
local Players = game:GetService("Players")
On this next line:
player.PlayerAdded:Connect(function(char)
I think you meant to connect to the PlayerAdded of the Players service, since a Player object itself doesn’t have this event. Also, the PlayerAdded event does not return a character Model, but rather a Player object. First, create a new function which would contain the code handling each new player:
local function onPlayerAdded(player: Player)
-- This will be where the code handles a new player joining the game
end
Then, connect the PlayerAdded event to it:
Players.PlayerAdded:Connect(onPlayerAdded)
Also, probably a good idea to do what @Ziffix did in their code and add this before connecting to the event:
for _, player in Players:GetPlayers() do
task.defer(onPlayerAdded, player)
end
Now, on to this piece of code:
local humanoid = char:WaitForChild("Humanoid")
while true do
wait(0.1)
humanoid.Health = player.leaderstats.Health.Value
end
This is an incorrect implementation. First of all, since PlayerAdded doesn’t actually give you the character model, the first line is guaranteed to infinitely yield, since Player objects never get a humanoid parented inside of them normally. Second, using an infinite loop to assign the health property is bad practice, as using events is much more preferred.
There’s also a little confusion with what you actually meant by making the “players health a leaderstat”. To me, this could mean 2 things:
- You want to have a Health leaderstat which would dictate (set) the amount of health the player has, which is what your broken code seems to be trying to do and what @Ziffix 's code snippet also does.
- You want to have a Health leaderstat which listens to and updates with the amount of health the player has. This seems to be more realistic to use for a game.
Again, I don’t know which exact one you’re going for, so I’m just gonna solve for both of them.
First, we need to get the player’s leaderstats folder. If your code already adds the leaderstats folder in a different script, then you just do what @Ziffix suggested:
local leaderstats = player:WaitForChild("leaderstats")
However, if leaderstats isn’t added anywhere, you have to do this:
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
(Reminder that this code is within the onPlayerAdded function)
Next, we need to get the “Health” NumberValue. Again, if another script already adds it, then:
local health = leaderstats:WaitForChild("Health")
If it doesn’t:
local health = Instance.new("NumberValue")
health.Name = "Health"
health.Parent = leaderstats
Now we need to do the actual tracking implementation. Since we need the Player’s Humanoid for either of my guesses, we need to do a bit of connecting to events, which ends up as this:
local function onHumanoidAdded(humanoid: Humanoid)
-- we have the player's humanoid here!
end
local function onCharacterAdded(character: Model)
local humanoid = character:WaitForChild("Humanoid")
onHumanoidAdded(humanoid)
end
if player.Character then
onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
The onHumanoidAdded function will have the Humanoid of the Player’s current character. We add code into into it depending on my 2 guesses:
1st guess, the value of the Health NumberValue sets the health of the humanoid:
local function onHumanoidAdded(humanoid: Humanoid)
humanoid.Health = health.Value
local changedConnection; changedConnection = health.Changed:Connect(function()
if humanoid.Parent == nil then
changedConnection:Disconnect()
return
end
humanoid.Health = health.Value
end)
end
2nd guess, the Health NumberValue just shows the player’s current health:
local function onHumanoidAdded(humanoid: Humanoid)
local function onHealthChanged(newHealth: number)
health.Value = newHealth
end
onHealthChanged(humanoid.Health)
humanoid.HealthChanged:Connect(onHealthChanged)
end
With all these changes made, here’s the final code, based on the 2nd guess and that the leaderstats are not created beforehand:
local Players = game:GetService("Players")
local function onPlayerAdded(player: Player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local health = Instance.new("NumberValue")
health.Name = "Health"
health.Parent = leaderstats
local function onHumanoidAdded(humanoid: Humanoid)
local function onHealthChanged(newHealth: number)
health.Value = newHealth
end
onHealthChanged(humanoid.Health)
humanoid.HealthChanged:Connect(onHealthChanged)
end
local function onCharacterAdded(character: Model)
local humanoid = character:WaitForChild("Humanoid")
onHumanoidAdded(humanoid)
end
if player.Character then
onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
end
for _, player in Players:GetPlayers() do
task.defer(onPlayerAdded, player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
tl;dr code had a lot of errors, I explained them all and the final code is above
