I’m trying to make this script print the names of all players so I can Check each players health. Please help!
wait(1)
for i, v in pairs(game.Players:GetPlayers()) do
print(i)---player priority
print(v)---players name
if v.Character.Humanoid.Health < 1 then
print("mjvjhgk")
end
end
for i, v in pairs(game.Players:GetChildren()) do
print(i)---player priority
print(v.Name)---players name
if v.Character.Humanoid.Health < 1 then
print("mjvjhgk")
end
end
This will work, you tried to print an object value, not his name.
for i, v in pairs(game.Players:GetPlayers()) do
print(v)---players name
print(v:WaitForChild("Character").Humanoid.Health).."/"..v:WaitForChild("Character").Humanoid.MaxHealth)
end
Your only printing ‘mjvjhgk’ when health is less than 1.
Your not actually printing the health itself, just checking if its less than 1.
You haven’t accounted for a character not being there.
Amendments to your script:
for i, v in pairs(game.Players:GetPlayers()) do
print(i) -- Order in List, this is not "priority" as you like to call it.
print(v) -- Player instance, if you print this it wont return the name
print(v.Name) -- Player instance's name
if v.Character then -- if this doesn't pass for a player then a character for them has not been loaded yet.
print(v.Character.Humanoid.Health) -- this will print the current health of a character.
end
end
Recommended amendments:
for _, Player in pairs(game.Players:GetPlayers()) do -- Renamed variables to make code easier to understand.
print(Player.Name) -- Player instance's name
if Player.Character then -- if this doesn't fire for a player then a character for them has not been loaded yet.
print(Player.Character.Humanoid.Health) -- this will print the current health of a character.
end
end
I tried it and it gave me a falled to search error, so I added a wait time to the waitforchild and that errored too.
for i, v in pairs(game.Players:GetPlayers()) do
print(i)---player priority
print(v)---players name
if v:WaitForChild("Character", 1).Humanoid.Health < 1 then
print("Big boi in da house")
end
end
Using the CharacterAdded event would not work in his instance, he wants them printed together through one script and not separately through many scripts.
What @6Clu is correct and would work, the only problem with it is it would ignore a player if he’s character hasn’t loaded yet, which is why nothing is being printed, so here is another suggestion.
for _, Player in pairs(game.Players:GetPlayers()) do
print(Player.Name) -- Player instance's name
local character = v.Character or v.CharacterAdded:Wait() --youll find yourself using this line a lot when waiting for a character to load
print(character.Humanoid.Health)
end
The Humanoid may or may not be inserted yet with CharacterAdded, best to use CharacterAppearanceLoaded since that fires when their full appearance (accessories, shirts etc) are loaded.