So I’m working on a map rotation system for my randomizer and the script is supposed to grab all players to load their characters once the next map is loaded, but instead it chooses to work for only the first person to join… This also goes for a gui that is supposed to display a round timer as well as the map name. But it just chooses to not work… Any help would be appreciated as I am still kind of an inexperienced scripter.
local Lobbies = game.ReplicatedStorage.Lobbies:GetChildren()
local FFAMaps = game.ReplicatedStorage.Maps.FFA:GetChildren()
timer = 0
wait()
game.ReplicatedStorage.UseLoadingScreen:Fire()
wait(7.5)
while true do
local ChosenLobby = Lobbies[math.random(1,#Lobbies)]:Clone()
ChosenLobby.Parent = workspace
wait(7.5)
ChosenLobby.Sounds.PlaySounds.Disabled = false
ChosenLobby.Scripts.EnableScripts.Disabled = false
game.Lighting.Ambient = ChosenLobby.Values.Lighting.Ambient.Value
game.Lighting.ClockTime = ChosenLobby.Values.Lighting.ClockTime.Value
game.Lighting.Brightness = ChosenLobby.Values.Lighting.Brightness.Value
game.Lighting.ExposureCompensation = ChosenLobby.Values.Lighting.ExposureCompensation.Value
game.Lighting.FogColor = ChosenLobby.Values.Lighting.FogColor.Value
game.Lighting.FogEnd = ChosenLobby.Values.Lighting.FogEnd.Value
ChosenLobby.Values.Lighting.Sky.Parent = game.Lighting
workspace.RandomItemScript.WeaponsAllowed.Value = false
for i, plr in ipairs(game.Players:GetChildren()) do
plr:LoadCharacter()
game.Players.CharacterAutoLoads = true
plr.PlayerGui:FindFirstChild(“RoundInfo”).MapName.Text = ChosenLobby.Values.LobbyName.Value
timer = 45
for i=1,timer do
timer = timer - 1
plr.PlayerGui:FindFirstChild(“RoundInfo”).TimeLeft.Text = timer
wait(1)
end
game.ReplicatedStorage.UseLoadingScreen:Fire()
workspace.Storage:ClearAllChildren()
game.Players.CharacterAutoLoads = false
plr.Character:Destroy()
ChosenLobby:Destroy()
game.Lighting:FindFirstChild(“Sky”):Destroy()
local ChosenMap = FFAMaps[math.random(1,#FFAMaps)]:Clone()
ChosenMap.Parent = workspace
wait(7.5)
workspace.RandomItemScript.WeaponsAllowed.Value = true
plr:LoadCharacter()
game.Players.CharacterAutoLoads = true
ChosenMap.Sounds.PlaySounds.Disabled = false
ChosenMap.Scripts.EnableScripts.Disabled = false
game.Lighting.Ambient = ChosenMap.Values.Lighting.Ambient.Value
game.Lighting.ClockTime = ChosenMap.Values.Lighting.ClockTime.Value
game.Lighting.Brightness = ChosenMap.Values.Lighting.Brightness.Value
game.Lighting.ExposureCompensation = ChosenMap.Values.Lighting.ExposureCompensation.Value
game.Lighting.FogColor = ChosenMap.Values.Lighting.FogColor.Value
game.Lighting.FogEnd = ChosenMap.Values.Lighting.FogEnd.Value
ChosenMap.Values.Lighting.Sky.Parent = game.Lighting
plr.PlayerGui:FindFirstChild(“RoundInfo”).MapName.Text = ChosenMap.Values.MapName.Value
timer = ChosenMap.Values.Game.RoundTime.Value
for i=1,timer do
timer = timer - 1
plr.PlayerGui:FindFirstChild(“RoundInfo”).TimeLeft.Text = timer
wait(1)
end
game.ReplicatedStorage.UseLoadingScreen:Fire()
workspace.Storage:ClearAllChildren()
game.Players.CharacterAutoLoads = false
plr.Character:Destroy()
ChosenMap:Destroy()
game.Lighting:FindFirstChild(“Sky”):Destroy()
end
end
Try to format your code with PreformatedText feature (Ctrl + E) so everyone can read it easily
local Lobbies = game.ReplicatedStorage.Lobbies:GetChildren()
local FFAMaps = game.ReplicatedStorage.Maps.FFA:GetChildren()
timer = 0
wait()
game.ReplicatedStorage.UseLoadingScreen:Fire()
wait(7.5)
while true do
local ChosenLobby = Lobbies[math.random(1,#Lobbies)]:Clone()
ChosenLobby.Parent = workspace
wait(7.5)
ChosenLobby.Sounds.PlaySounds.Disabled = false
ChosenLobby.Scripts.EnableScripts.Disabled = false
game.Lighting.Ambient = ChosenLobby.Values.Lighting.Ambient.Value
game.Lighting.ClockTime = ChosenLobby.Values.Lighting.ClockTime.Value
game.Lighting.Brightness = ChosenLobby.Values.Lighting.Brightness.Value
game.Lighting.ExposureCompensation = ChosenLobby.Values.Lighting.ExposureCompensation.Value
game.Lighting.FogColor = ChosenLobby.Values.Lighting.FogColor.Value
game.Lighting.FogEnd = ChosenLobby.Values.Lighting.FogEnd.Value
ChosenLobby.Values.Lighting.Sky.Parent = game.Lighting
workspace.RandomItemScript.WeaponsAllowed.Value = false
for i, plr in ipairs(game.Players:GetChildren()) do
plr:LoadCharacter()
game.Players.CharacterAutoLoads = true
plr.PlayerGui:FindFirstChild(“RoundInfo”).MapName.Text = ChosenLobby.Values.LobbyName.Value
timer = 45
for i=1,timer do
timer = timer - 1
plr.PlayerGui:FindFirstChild(“RoundInfo”).TimeLeft.Text = timer
wait(1)
end
game.ReplicatedStorage.UseLoadingScreen:Fire()
workspace.Storage:ClearAllChildren()
game.Players.CharacterAutoLoads = false
plr.Character:Destroy()
ChosenLobby:Destroy()
game.Lighting:FindFirstChild(“Sky”):Destroy()
local ChosenMap = FFAMaps[math.random(1,#FFAMaps)]:Clone()
ChosenMap.Parent = workspace
wait(7.5)
workspace.RandomItemScript.WeaponsAllowed.Value = true
plr:LoadCharacter()
game.Players.CharacterAutoLoads = true
ChosenMap.Sounds.PlaySounds.Disabled = false
ChosenMap.Scripts.EnableScripts.Disabled = false
game.Lighting.Ambient = ChosenMap.Values.Lighting.Ambient.Value
game.Lighting.ClockTime = ChosenMap.Values.Lighting.ClockTime.Value
game.Lighting.Brightness = ChosenMap.Values.Lighting.Brightness.Value
game.Lighting.ExposureCompensation = ChosenMap.Values.Lighting.ExposureCompensation.Value
game.Lighting.FogColor = ChosenMap.Values.Lighting.FogColor.Value
game.Lighting.FogEnd = ChosenMap.Values.Lighting.FogEnd.Value
ChosenMap.Values.Lighting.Sky.Parent = game.Lighting
plr.PlayerGui:FindFirstChild(“RoundInfo”).MapName.Text = ChosenMap.Values.MapName.Value
timer = ChosenMap.Values.Game.RoundTime.Value
for i=1,timer do
timer = timer - 1
plr.PlayerGui:FindFirstChild(“RoundInfo”).TimeLeft.Text = timer
wait(1)
end
game.ReplicatedStorage.UseLoadingScreen:Fire()
workspace.Storage:ClearAllChildren()
game.Players.CharacterAutoLoads = false
plr.Character:Destroy()
ChosenMap:Destroy()
game.Lighting:FindFirstChild(“Sky”):Destroy()
end
end
I think you should rebuild the entire approach you are using, handle stuff with functions outside the main function you are showing, handling playerAdded, playerRemoving, creating variables that holds all those references you repeat a lot of times and handling them with functions, I think that code is a lil messy.
The reason why its “stopping” or only “working for first player”, its because this part:
for i, plr in ipairs(game.Players:GetChildren()) do
plr:LoadCharacter()
game.Players.CharacterAutoLoads = true
plr.PlayerGui:FindFirstChild(“RoundInfo”).MapName.Text = ChosenLobby.Values.LobbyName.Value
timer = 45
-- THIS PART
for i=1,timer do
timer = timer - 1
plr.PlayerGui:FindFirstChild(“RoundInfo”).TimeLeft.Text = timer
wait(1)
end
Once you started with the iteration of all players, (which should be game.Players:GetPlayers()
instead of GetChildren()
) once you started with the loop, the first player found starts a for loop
that decrease the timer = 45
variable each 1 second, and by using wait(1)
that will “stop” the script, until 45 reach 0, and only for that first player, once the 45 seconds are over, the for loop
will start with the second player doing the same, you could solve it by using spawn()
for the for loop
… but I dont recomend it unless you fix the entire code…