Is There A Better Way To Do This

  1. What do you want to achieve? I Want It To Be As Safe As Possible I’m Just Thinking What If The Player Failed To Teleport.

  2. What is the issue? There’s No Issue But Sometime When I Teleport To The Game It Doesn’t Fire

  3. What solutions have you tried so far? I Tried Waiting For 10 Second Before Firing But What If The Player Joined Late

while wait(1) do
	for i,v in pairs(game.Players:GetPlayers()) do
		task.spawn(function()
			if not table.find(playerTable, v.Name) then
				local Character = v:LoadCharacter()
				playersNeeded = playersNeeded + 1
				table.insert(playerTable,v.Name)
			end
		end)
	end
	if playersNeeded == #game.Players:GetPlayers() then
		print("Test")
		startGame.Play()
		break
	end
end
1 Like

What can you tell me more about it?

That while loop is useless and you can save up resources by replacing it with the PlayerAdded and PlayerRemoving events.

local playersNeeded = 5 -- minimum number of players change it as you please

game.Players.PlayerAdded:Connect(function(player)
    if not table.find(playersTable, player.UserId then -- it is generally better to use user ids rather than usernames in these situations
        -- your v is now player
        -- load character
        -- increasing the playersNeeded var is redundant
    end

    if playersNeeded == #playersTable then
        -- start the game
        table.clear(playersTable) -- reset for a new round
    end
end)

game.Players.PlayerRemoving:Connect(function(player) 
    -- check if the player is in playersTable and if yes then remove them
end)

In this case though using a table is useless since it is going to add all the players in the game anyway.

2 Likes

I don’t understand what you are trying to do via your description? Are you trying to teleport a single player somewhere and have a fall back in case it fails?

no there can be 4 players max and i want the game to wait for all the player that is teleporting and incase someone failed to teleport it will detect it and not wait for that one person

what if one person failed to teleport to the game?

Ok, so when an experience starts, it should wait for at least 4 players to join? And if 4 players don’t join within a certain time, then it should do something else?

as you can see in the script i placed a while wait do loop but how do i make a loop if player dont join within a certain time it stop the loop

You would have to do that completely different than how you have it setup now. If the goal is to count players as they enter the server and then after a timeout, start the experience, you have more variables to account for than just the number of players like experience loading delay, experience running load, latency for connecting players, etc. Just using a raw 10 second timer will only work in ideal, testing circumstances and not reflect how live servers behave.

1 Like

You can use the Players.PlayerAdded and Players.PlayerRemoving event to solve this issue!

See more here: Players | Roblox Creator Documentation

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.