Trying to setup a script which waits until all players load, it consists of selecting who will be “The Mother”, loading data with profile service, then inserting the player after all that in a table, checking if the #table >= Players:GetPlayers() then setting an attribute to workspace telling it it’s all loaded. I was wondering if this is a good way of doing it?
local function onPlayerAdded(Player: Player)
DataManager.OnPlayerAdded(Player)
determineMother()
repeat wait()
until Mother
local Character = Player.Character or Player.CharacterAdded:Wait()
onCharacterAdded(Player,Character)
Player.CharacterAdded:Connect(function(char)
onCharacterAdded(Player,char)
end)
table.insert(loadedPlayers, Player)
if #loadedPlayers >= #Players:GetPlayers() then
print("everyone loaded")
workspace:SetAttribute("Loaded", true)
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(function(Player)
DataManager.OnPlayerRemoved(Player)
if loadedPlayers[Player] then
for _, player in loadedPlayers do
if player == Player then
player = nil
end
end
end
end)
game:BindToClose(function()
for _, player in Players:GetPlayers() do
DataManager.OnPlayerRemoved(player)
en
workspace.AttributeChanged:Connect(function(attribute)
if attribute == "Loaded" then
print("Everything loaded, continue the game.")
end
end)
I don’t know how the function determineMother but the way you have it set up at the moment is it will be called every time a player joins, and I suppose only one player should be a mother right?
I’m trying to think of an easy solution, but what you could do is give the player’s character a BoolValue named ‘isLoaded’ when they first join the game.
Example (could error, since I am writing this in the forums) :
local function onPlayerAdded(player)
local isLoaded = Instance.new("BoolValue")
isLoaded.Name = "isLoaded"
isLoaded.Value = false
isLoaded.Parent = player
local loaded = player:HasAppearanceLoaded()
while not loaded do
loaded = player:HasAppearanceLoaded()
--//print("Waiting for " .. player.Name .. "'s character to load.)
task.wait()
end
player.isLoaded.Value = true
end
From there you can go further, either adding them in a list, or simply dropping the BoolValue and just waiting until the loop is passed (meaning the character has loaded) and then continue with the script. The BoolValue serves more of a purpose to make it possible to communicate if the character has loaded to other scripts listening to that value.
Yeah, you’re right. It’s just a function which takes players, selects random mother, and begins rounds, only when all players loaded.
local function determineMother()
local AllPlayers = Players:GetChildren()
local currentPlayers = #Players:GetPlayers()
if currentPlayers < 1 then
warn("Not enough players joined, calling return function.")
return
end
if selectionThread then
task.cancel(selectionThread)
selectionThread = nil
end
selectionThread = task.delay(3, function()
local randomPlayerIndex = math.random(1, currentPlayers)
Mother = AllPlayers[randomPlayerIndex]
end)
end