GetPlayers returning userdata and not player

Hello, i’m currently having an issue with the Players:GetPlayers function. When i call it, it returns userdata and not player object which create an error when i try to fire client.

The error stats that it is expecting a player object for fire client.

Thanks for the help!

Could you send your code so I can help you?

print(game.Players:GetPlayers())

local TestRemote = Instance.new(“RemoteEvent”)

TestRemote.Parent = game.ReplicatedStorage

TestRemote:FireClient(game.Players:GetPlayers()[1])

Ignore that the code is bad and dont do anything. I’m using it to test something to see if it even works.

Are you trying to fire the remote for all clients or just one?

Players:GetPlayers() returns an array containing all the players, not a single player, you have to loop through the array to apply code for each player:

for _, player in pairs(Players:GetPlayers()) do 
	print(player.Name)
end
1 Like

As i said this is a code for testing so it is normal that it is only getting the first player(Me).

In this case, you can easily use the following code

local TestRemote = Instance.new(“RemoteEvent”)

TestRemote.Parent = game.ReplicatedStorage

game.Players.PlayerAdded:connect(function(Player)
TestRemote:FireClient(Player)
end)

1 Like

The issue is, that the code does not see the Player it sees an array with a single value which is the player.

1 Like

If you look in the code, i do GetPlayers()[1] which gets the first player in the array.

It still does the same thing. It’s very weird.

Can you give this a try? It should work.

local TestRemote = Instance.new('RemoteEvent', game.ReplicatedStorage)

repeat wait(1) until #game.Players:GetPlayers() ~= 0 -- waiting until there's at least 1 player

TestRemote:FireClient(game.Players:GetPlayers()[1])
1 Like

The real code, which uses classes is not working because the self is not existant outside the .new constructor which cause the problem.

Thanks for the help anyway.

1 Like