For _,v in ipairs(game.Players:GetPlayers()) do not getting every player

Hello I am making a game but I got a problem when in the script it says:

for _,v in ipairs(game.Players:GetPlayers())

It doesn’t get all players and I am trying to change the text for all players and teleport all players somewhere. Heres the script:

while true do
	wait(3)
	script.LobbyMusic:Play()
	for _,v in ipairs(game.Players:GetPlayers()) do
		local Text = v.PlayerGui.GameHUD.TextLabel
		Text.Text = "A game of  Game will start soon."
		wait(20)
		Text.Text = "Game will start in 5"
		wait(1)
		Text.Text = "Game will start in 4"
		wait(1)
		Text.Text = "Game will start in 3"
		wait(1)
		Text.Text = "Game will start in 2"
		wait(1)
		Text.Text = "Game will start in 1"
		wait(1)
		Text.Text = "Game has started"
		v.IsPlaying.Value = true
		script.Players.Value = game.Players.NumPlayers
		script.LobbyMusic:Stop()
		v.Character.HumanoidRootPart.Position = game.Workspace.Spawn1.Position
		wait(12121123123123123)
	end
end

ye so how do i fix this?

1 Like

Because the way you designed your code prevents changes from happening on all players at once, you should use localscripts with RemoteEvent.OnClientEvent and serverscript containing RemoteEvent:FireAllClients() to change text on GUIs instead.

1 Like

You are putting a for loop in a while loop
the reason why its not getting all the player is because of all the wait(), so the for loop is slower
And you should use remotevents fired to server to teleport all players
It’s better to have round scripts in the server too and have it fire to clients
And for that for loop use pairs instead of ipairs maybe?

1 Like
  1. I removed the wait() and it said something like script exhausted timeout.
  2. I replaced ipairs to pairs and it still does not get all players.

The original text in the textlabel is Intermission and it changes to “A game of Game will start soon.” by the script and when I test it with 2 players for 1 it shows Intermission and for the other it shows what its meant to show.

1 Like

no i meant this should be a serverscript, and instead of changing the guis by looping Players, you should use remoteEvents and fire to client

add a wait in the for loop obviously

1 Like

still didnt work idk why its not workinggg

1 Like

Is this local or server?

1 Like

it is a serverscript and when I stayed in the game longer and the other players text got changed and they teleported but i had to wait like 30 seconds i want it to teleport and change text for everyone in the server at the same time.

Use remoteEvents to change it for everyone. I have said that a couple of times.

2 Likes

I’d suggest you to use coroutines.
The problem you are facing is the wait time.
First, you have to understand how an ipairs/pairs loop works.
Say there are 3 objects in a model (I will make them strings for now)

local objects = {"first", "second", "third"}

You want to print all of them and have a wait(5) after it printed

local objects = {"first", "second", "third"}

for i,v in ipairs(objects) do
   print(v)
   wait(5)
end

Now what this would do is following: It would print “first”, wait 5 seconds and only then print “second” and so on.
It first does his first task, only then the second. They don’t run at the same time.
I’d recommend using coroutines if you want to achieve that.
A coroutine creates a new thread.

local objects = {"first", "second", "third"}

for i,v in ipairs(objects) do
   coroutine.wrap(function()
      print(v)
      wait(5)
   end)()
end

Would work.
Notice how I put () after the end), it’s just for the execution (you execute a function with () ).
So in your case, you would want to do

while true do
	wait(3)
	script.LobbyMusic:Play()
	for _,v in ipairs(game.Players:GetPlayers()) do
        coroutine.wrap(function()
		local Text = v.PlayerGui.GameHUD.TextLabel
		Text.Text = "A game of  Game will start soon."
		wait(20)
		Text.Text = "Game will start in 5"
		wait(1)
		Text.Text = "Game will start in 4"
		wait(1)
		Text.Text = "Game will start in 3"
		wait(1)
		Text.Text = "Game will start in 2"
		wait(1)
		Text.Text = "Game will start in 1"
		wait(1)
		Text.Text = "Game has started"
		v.IsPlaying.Value = true
		script.Players.Value = game.Players.NumPlayers
		script.LobbyMusic:Stop()
		v.Character.HumanoidRootPart.Position = game.Workspace.Spawn1.Position
		wait(12121123123123123)
        end)()
	end
end

Now the game just glitches. The music stops and starts and it teleports me infinity.

Yoo it worked thanks alot! I was about to give up

1 Like