for some reason, this script doesn’t work. it won’t even print. i don’t know what’s wrong. any help is appreciated.
Script:
for i, player in pairs(game.Players:GetPlayers()) do
if player.Character then
print(player.Name)
end
end
for some reason, this script doesn’t work. it won’t even print. i don’t know what’s wrong. any help is appreciated.
Script:
for i, player in pairs(game.Players:GetPlayers()) do
if player.Character then
print(player.Name)
end
end
Where is this script located? Like is it in ServerScriptService, StarterPlayerScripts, etc…
its located in serverscriptservice.
Well since it’s a serverscript, it’s gonna be running before any clients have even joined.
Since that’s the case, it only goes through the players once. Even if a player has joined when its through the loop, the character might not.
I’d use ChildAdded() to tell when a player is added to Players, as well as use Player.CharacterAdded() to tell when a character spawns in
Hope I helped
i tried this:
for i, player in pairs(game.Players:GetPlayers()) do
if player.CharacterAdded:Wait() then
print(player.Name)
end
end
but still no printing
--< Variables >--
local Players = game["Players"]
--< Functions >--
function PlayerAdded(user)
user.CharacterAdded:Connect(function()
print("Character added for" .. user.Name .. "!")
end
end
--< Functions >--
Players.PlayerAdded:Connect(PlayerAdded)
Like I said, It is looking through the list only once; and when it looks through, there are no players… As most serverscripts are ran before the client is even in.
is there a way to loop through all the players? I’m working on something.
Yes, Your code should work if it’s running at a time when players are already in the server.
However, in this code, there aren’t players in the server because its running before any join.
task.wait(3)
local players = game:GetService("Players")
for _, player in pairs(players:GetPlayers()) do
if player.Character then
print(player.Name)
end
end
I am like 100% sure you dont need the “player.character”, just player suffices.
Just looking to stick with his script as close as possible. You’re probably right.
I was actually looking at that for a bit … thinking why.
Almost posted:
player.Character or player.CharacterAdded:Wait()
Thinking it might be there for a reason I’m not aware of.
This should work.
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
for i, player in players:GetChildren()do
if player then
print(player.Name)
end
end
end)
The for
should work, but it should also print 0 because no players have even joined yet, it runs too quickly.
In this example, it checks all players when someone says “/getPlayers”
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if message == "/getPlayers" then
print(tostring(#game.Players:GetPlayers()).." Players")
print("- - -")
for index,player in pairs(game.Players:GetPlayers()) do
print(tostring(index)..": "..tostring(player.Name))
end
end
end)
end)
well, you could just add a localscript in starterplayerscripts and run this:
print(game.Players.LocalPlayer.Name)
if you want to keep server side for something else
function printP(player: Player)
if player == nil then warn("Player not provided!") return end
if not player:IsA("Player") then warn("Not a player!") return end
print(player.Character and player.Name or "Character hasn't loaded.")
end
for _, v in game.Players:GetPlayers() do
printP(v)
end
game.Players.PlayerAdded:Connect(printP)
The others are right the script runs before the players, so what you can do is;
local players = game:GetService("Players")
function loopPlayers()
for i, v in pairs(players:GetPlayers()) do
if v.Character then
print(v.Name)
end
end
end
players.PlayerAdded:Connect(loopPlayers)
this one loops the players every time someone joins.
You don’t actually need to run the loop inside the function.
this is probably because the script ran before there are any players in the server.
-- wait for any players.
game.Players.PlayerAdded:Wait()
for i, player in pairs(game.Players:GetPlayers()) do
if player.Character then
print(player.Name)
end
end
i put it there maybe s/he may add a .PlayerRemoved one as well
This doesn’t guarantee the player will be in the game, and isn’t a way to go about this in the first place.