Today I have a problem that hopefully someone can help me fix it.
Alright so to explain this, I have given the players who are playing the game a BoolValue called “Player” and I’m trying to start the game once there are 2 players with a repeat until loop that repeats the code inside of it until there are two players in the game that have the BoolValue “Player” have this BoolValue but I can’t seem to do it, can anyone help?
Here’s the code:
Code
repeat
GameState.Value = "Starting at 2 players.."
wait(1)
until #game:GetService("Players"):GetChildren():FindFirstChild("Player") >= 2
As you can see the code is simple but I can’t get it to work I will try and explain it to you more with the explanation that I have gave you above so you can have a better understanding of what I’m doing:
The code inside of the Repeat Until loop is basically just updating a TextLabel to say that the game needs 2 players to start, I’m aware that :GetChildren() returns a table by the way, and what I’m trying to do is to find the BoolValue called “Player” inside of the players and if there’s 2 or more of that BoolValue then the game is going to start, maybe the code isn’t working because I need to get what’s inside of the :GetChildren() table?
I’m using the # operator to get the amount of the available “Player” BoolValues.
I have tried to do everything before coming here, like trying in many different ways to find the “Player” BoolValue inside of the players but I just can’t do it so I need help, how do I go about doing this?
I’m NOT asking people to make scripts for me but I just want you to either tell me how do I go about doing it or you can help me fix it if it’s alright but there’s a small error, also please tell me what I’m doing wrong here so I can avoid doing it next time, I don’t know how to fix this but I have been trying to fix it, I have looked everywhere including the Roblox Developer API Reference for help on this.
You’re attempting to call the method FindFirstChild on a table. GetChildren() returns an array (or table) of children inside the object you called it on, in this case, the Players Service.
You would need to make a custom method to do this e.g.
local getPlaying = (function ()
local count = 0
for i, v in next, game.Players:GetChildren() do --> for each player 'v' at index 'i' in the children list of Players
if v:FindFirstChild "Player" then --> if that player 'v' has a child called 'Player' then add to count
count = count + 1
end
end
return count
end)
repeat
GameState.Value = "Starting at 2 players.."
wait(1)
until getPlaying() >= 2 --> repeat until the number of people with the Player child is greater or equal than 2
I don’t want you to just put a random script here without understanding exactly what each line of code does, I would like to know what you did that differentiates my script from yours, and why won’t my code work with the # operator? also you could just tell me what to do instead.
In regard to why yours won’t work, as I said above: the GetChildren() method returns a list of the children of the object you called it on. You’re unable to call the method FindFirstChild on that table because that is a method of World objects - calling the len (#) operator on the result of these two calls is like calling #nil
You need to loop through the returned GetChildren table, and for each child in that list, perform the FindFirstChild method to count the number of objects within that list that contain the Players object.
The ‘next’ function is just a stateless iterator, the ‘pairs’ function actually uses the ‘next’ function to iterate. More information can be found here: Programming in Lua : 7.3
I purely use it for syntactic sugar, but it’s the exact same as doing:
for i, v in pairs(table) do
print(i, v)
end
Hope that’s provided you with a comprehensive solution.