Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Lobby Script minimum number of players not working?

Asked by 5 years ago

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.

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

This is the main server script ^

1game.Players.PlayerAdded:Connect(function(player)
2    player.CharacterAdded:Connect(function(modelPlayer)
3        local playing = Instance.new("BoolValue")
4        playing.Name = "playing"
5        playing.Parent = player
6        playing.Value = false
7    end)
8end)

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!

0
I’m sure the while loop won’t run if “playable” is true. Therefore those “if” recursers won’t run. SmartNode 383 — 5y
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. RIBBENTROPP 45 — 5y
0
Don't use instances to tag players, use collection service dang it... User#24403 69 — 5y
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 ? RIBBENTROPP 45 — 5y
View all comments (3 more)
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. User#24403 69 — 5y
0
Thanks, appreciate it RIBBENTROPP 45 — 5y
0
< 2 which means 1 or less. Line 15. I looked at it the moment I opened this thread. greatneil80 2647 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
1for i = 1, # players do
2        if players[i]:FindFirstChild("playing") then
3            local playing = players[i].playing.Value
4            if playing == true then
5                table.insert(playingPlayers, players[i])
6            end
7        end
8    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

0
Thanks! This worked, so simple too RIBBENTROPP 45 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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

1local players = game:GetService("Players")
2 
3repeat wait() until #game.Players:GetPlayers() >= 2 --Put What Ever Amount Of Player You Want
4 
5print('Enough Players!')
6 
7wait()

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.

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 RIBBENTROPP 45 — 5y

Answer this question