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.

    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!

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
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

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

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.

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