Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Lobby Script minimum number of players not working?

I have created a script which assigns the boolean 'playing' to each player when the enter the server. When they click the GUI button 'start' this changes to true, however this is not the problem.

My main server script for the execution of the game calls an error because (as far as I am aware) the script is performed on the player before my other script assigns the 'playing' bool to them when they join. I have a bad workaround this by adding a temporary 'wait' for 10 seconds for them to load in fully and be assigned the 'playing' bool but this still crashes if another player joins after that first 10 seconds as it constantly loops through checking if the players have the variable.

	playable = false	
	--wait(10) --So the game has time to assign 'playing' to players
	while not playable do	
	--This creates a new array of players who have actually clicked "start", we don't want the hunter to be in the main menu.
	players = game.Players:GetChildren() -- String array where we store all the players
	playingPlayers = {}
	for i = 1, # players do
		local playing = players[i].playing.Value
		if playing == true then
			table.insert(playingPlayers, players[i])
		end
	end
	lobbyAmount = tablelength(playingPlayers)
	wait()
		if lobbyAmount < 2 then
			print("Not enough players")
		else
			playable = true
		end
	end	

This is the main server script ^

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(modelPlayer)
		local playing = Instance.new("BoolValue")
		playing.Name = "playing"
		playing.Parent = player
		playing.Value = false
	end)
end)

This is the other script that assigns the bool to the player. ^

It crashes on 'local playing = players[i].playing.Value as when the player first joins the other script doesn't have time to assign that variable to them (I think that's what is happening anyway). I'm pretty stuck on how to solve this, any help would be appreciated thanks!

7 comments

0

I’m sure the while loop won’t run if “playable” is true. Therefore those “if” recursers won’t run.

0

The while loop definitely runs, as playable is set to false at the start and the while loop runs for as long as playable is set to false. I get an error at line 08 so that's the problem, maybe I didn't explain it well enough in the post.

0

Don't use instances to tag players, use collection service dang it...

0

I'm pretty new to lua, so sorry if I'm doing something that's frowned upon, I've never heard of collection service before, how would it interact differently ?

0

I write round based scripts all the time. In part of it, I wait for there to be enough players in order to do something. I'll write that piece for you.

0

Thanks, appreciate it

0

< 2 which means 1 or less. Line 15. I looked at it the moment I opened this thread.

2 answers

✓ Accepted answer
for i = 1, # players do
        if players[i]:FindFirstChild("playing") then
            local playing = players[i].playing.Value
            if playing == true then
                table.insert(playingPlayers, players[i])
            end
        end
    end

You need to add an if statement to check if the value actually exists, and if it does then you create the variable, not before. This ensures the script does not crash

1 comment

0

Thanks! This worked, so simple too

I'm Still Really New To Lua And I Don't Fully Under stand The Question But This May Work, Sorry if It Doesn't

Also On The 2nd Line Was The --wait(10) An Accident Or Was -- In Your Script Aswell

local players = game:GetService("Players")

repeat wait() until #game.Players:GetPlayers() >= 2 --Put What Ever Amount Of Player You Want
	 
print('Enough Players!')

wait()

Maybe If You Add That To The Front Of Your First Shown Script, Again Sorry if This Is Compliantly Wrong Because I Am New To Lua.

1 comment

0

Not quite the code to solve the problem I was having I was looking for but it was still useful to read, I didn't know you could wait for an amount of players like that! Thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.