I have a game where you are teleported to a random obby map and you have to complete it to get coins. The problem is that it will only teleport one person to the map… Does anybody know how to fix it? Here is the code (ignore the smog thing):
local smog = game.ReplicatedStorage.Smog.Value
local status = game.StarterGui.StatusBar.TextLabel
local function killEveryone()
local allPlayers = game.Players:GetChildren()
for i = 1, #allPlayers do
allPlayers[i].Character.Humanoid.Health = 0
end
end
function gameStart()
local n = math.random(1,4)
local selectedMap = “Map”…n
print("The map is "…selectedMap)
local map = game.ReplicatedStorage.Maps:FindFirstChild(selectedMap):Clone()
wait(1)
print("Teleporting")
local players = game.Players:GetChildren()
map.Parent = workspace
if selectedMap == "Map1" then
if smog == true then
game.ReplicatedStorage.Maps.Map1["The Other Place"].ParticleThing.ParticleEmitter.Enabled = true
else
game.ReplicatedStorage.Maps.Map1["The Other Place"].ParticleThing.ParticleEmitter.Enabled = false
end
for i = 1, #players do
players[i].Character:MoveTo(Vector3.new(-203.035, 1.385, -83.845))
status.Text = "Selected Map is: Dino Dash"
wait(30)
status.Text = "Hurry up, only 10 seconds left!"
wait(10)
smog = false
killEveryone()
status.Text = "INTERMISION"
end
elseif selectedMap == "Map2" then
if smog == true then
game.ReplicatedStorage.Maps.Map2.ParticleThing.ParticleEmitter.Enabled = true
else
game.ReplicatedStorage.Maps.Map2.ParticleThing.ParticleEmitter.Enabled = false
end
for i = 1, #players do
players[i].Character:MoveTo(Vector3.new(-452.205, 1.36, 238.25))
status.Text = "Selected Map is: Facilty Freakout"
wait(40)
status.Text = "Hurry up, only 10 seconds left!"
wait(10)
smog = false
killEveryone()
status.Text = "INTERMISION"
end
elseif selectedMap == "Map3" then
if smog == true then
game.ReplicatedStorage.Maps.Map3.Baseplate.ParticleEmitter.Enabled = true
else
game.ReplicatedStorage.Maps.Map3.Baseplate.ParticleEmitter.Enabled = false
end
for i = 1, #players do
players[i].Character:MoveTo(Vector3.new(435.6, 1.765, -136.295))
status.Text = "Selected Map is: Hedge Maze Hurry"
wait(90)
status.Text = "Hurry up, only 10 seconds left!"
wait(10)
status.Text = "INTERMISION"
killEveryone()
smog = false
end
elseif selectedMap == "Map4" then
for i = 1, #players do
if smog == true then
game.ReplicatedStorage.Maps.Map4.ParticleThing.ParticleEmitter.Enabled = true
else
game.ReplicatedStorage.Maps.Map4.ParticleThing.ParticleEmitter.Enabled = false
end
players[i].Character:MoveTo(Vector3.new(424.71, 3.75, 78.53))
status.Text = "Selected Map is: Pyramid Puzzle"
wait(40)
status.Text = "Hurry up, only 10 seconds left!"
wait(10)
status.Text = "INTERMISION"
killEveryone()
smog = false
end
remember that loops are conducted in iterations and when you call “wait()” in the middle of an iteration, it makes the whole script wait because its all on 1 thread.
try doing something like
for i = 1, #players do
coroutine.wrap(function()
-- do stuff here.
end)()
end
local players = game.Players:GetChildren()
local smog = game.ReplicatedStorage.Smog.Value
local status = game.StarterGui.StatusBar.TextLabel
local function killEveryone()
for i, Player in pairs(players) do
Player.Character.Humanoid.Health = 0
end
end
function gameStart()
local n = math.random(1,4)
local selectedMap = n
print("The map is "..selectedMap)
local map = game.ReplicatedStorage.Maps:FindFirstChild(selectedMap):Clone()
wait(1)
print("Teleporting")
map.Parent = workspace
if selectedMap == "Map1" then
if smog == true then
game.ReplicatedStorage.Maps.Map1["The Other Place"].ParticleThing.ParticleEmitter.Enabled = true
else
game.ReplicatedStorage.Maps.Map1["The Other Place"].ParticleThing.ParticleEmitter.Enabled = false
end
for i, Player in pairs(players) do
if Player.Character then
Player.Character:FindFirstChild("HumanoidRootPart"):MoveTo(Vector3.new(-203.035, 1.385, -83.845))
status.Text = "Selected Map is: Dino Dash"
wait(30)
status.Text = "Hurry up, only 10 seconds left!"
wait(10)
smog = false
killEveryone()
status.Text = "INTERMISION"
end
end
elseif selectedMap == "Map2" then
if smog == true then
game.ReplicatedStorage.Maps.Map2.ParticleThing.ParticleEmitter.Enabled = true
else
game.ReplicatedStorage.Maps.Map2.ParticleThing.ParticleEmitter.Enabled = false
end
for i, Player in pairs(players) do
if Player.Character then
Player.Character:FindFirstChild("HumanoidRootPart"):MoveTo(Vector3.new(-452.205, 1.36, 238.25))
status.Text = "Selected Map is: Facilty Freakout"
wait(40)
status.Text = "Hurry up, only 10 seconds left!"
wait(10)
smog = false
killEveryone()
status.Text = "INTERMISION"
end
end
elseif selectedMap == "Map3" then
if smog == true then
game.ReplicatedStorage.Maps.Map3.Baseplate.ParticleEmitter.Enabled = true
else
game.ReplicatedStorage.Maps.Map3.Baseplate.ParticleEmitter.Enabled = false
end
for i, Player in pairs(players) do
if Player.Character then
Player.Character:FindFirstChild("HumanoidRootPart"):MoveTo(Vector3.new(435.6, 1.765, -136.295))
status.Text = "Selected Map is: Hedge Maze Hurry"
wait(90)
status.Text = "Hurry up, only 10 seconds left!"
wait(10)
status.Text = "INTERMISION"
killEveryone()
smog = false
end
end
elseif selectedMap == "Map4" then
for i, Player in pairs(players) do
if smog == true then
game.ReplicatedStorage.Maps.Map4.ParticleThing.ParticleEmitter.Enabled = true
else
game.ReplicatedStorage.Maps.Map4.ParticleThing.ParticleEmitter.Enabled = false
end
if Player.Character then
Player.Character:FindFirstChild("HumanoidRootPart"):MoveTo(Vector3.new(424.71, 3.75, 78.53))
status.Text = "Selected Map is: Pyramid Puzzle"
wait(40)
status.Text = "Hurry up, only 10 seconds left!"
wait(10)
status.Text = "INTERMISION"
killEveryone()
smog = false
end
end
this should be good, i did a bit of optimization too but its still not that optimized, but you should know what to do.
if selectedMap == "Map1" then
if smog == true then
game.ReplicatedStorage.Maps.Map1["The Other Place"].ParticleThing.ParticleEmitter.Enabled = true
else
game.ReplicatedStorage.Maps.Map1["The Other Place"].ParticleThing.ParticleEmitter.Enabled = false
end
for i = 1, #players do
players[i].Character:MoveTo(Vector3.new(-203.035, 1.385, -83.845))
end
status.Text = "Selected Map is: Dino Dash"
wait(30)
status.Text = "Hurry up, only 10 seconds left!"
wait(10)
smog = false
killEveryone()
status.Text = "INTERMISION"