I’m making a system to teleport the players to the map in my game, and the maximum number of players needed to start a game is 2. Every time I run the game, an error appears in the Output “attempt to compare number and instance”, and this is giving error on line 8
"if game.Players >= NeededPlayers then"
Here is my script.
local MapsFolder = ServerStorage:FindFirstChild("Maps")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GameHandler = ReplicatedStorage.StringValues:FindFirstChild("GameHandler")
local PickRandomMap = math.random(1)
local NeededPlayers = 2
if game.Players >= NeededPlayers then
for i = 10, -1 do
GameHandler.Value = "Intermission! "..i
wait(10)
GameHandler.Value = "Selecting map..."
if PickRandomMap == 1 then
MapsFolder.Isle:Clone().Parent = workspace
end
wait(5)
GameHandler.Value = "Teleporting players..."
end
end
Thank you in advance!
Change game.Players
to #game.Players:GetPlayers()
Reason: game.Players is a service, think of it as a folder for players. game.Players:GetPlayers() returns a table. Put a # in front of a table to get how many values are in it
4 Likes
Try using GetPlayers loop instead using game.Players
(you aslo can use GetChildren)
2 Likes
if (#game.Players.GetPlayers(game.Players) >= NeededPlayers) then
-- code
end
Effectively GetPlayers(DataModel::Players this)
is just the same as GetChildren(Instance<PlayersService> this)
, the difference is, Instead of returning an array of List<Instance<unknown>>
, it will return an array of the following: List<Instance<Player>>
.
Summary:
std::List<RBX::Instance<RBX::Instance.Any>> RBX::Instance::GetChildren(RBX::Instance<RBX::Instance.Any> this) {};
std::List<RBX::Instance<RBX::Player>> RBX::Instance<RBX::Players>::GetPlayers(RBX::Instance<RBX::Players> this) {}
2 Likes
Thank you very much, I was stuck in this for half an hour. LoL
Thank you so much, I was stuck in this for half an hour!