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