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
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.
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?
I removed the wait() and it said something like script exhausted timeout.
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.
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.
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