another mistake. this time, the selection of random two players does not work
local lobby = game.Workspace.Spawn
local map = game.ReplicatedStorage.Maps:GetChildren()
local status = game.ReplicatedStorage.Status
while true do
for i = 5, 0, -1 do
status.Value = "Intermission:"..i
wait(1)
end
local chosenmap = map[math.random(1, #map)]
local clonemap = chosenmap:Clone()
clonemap.Parent = game.Workspace
status.Value = "Map:"..clonemap.Name
wait(2.5)
local chosenplayer1 = game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())]
local chosenplayer2 = nil
repeat
wait()
chosenplayer2 = game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())]
until chosenplayer2 ~= chosenplayer1
chosenplayer1.Character:PivotTo(clonemap.Teleport1.CFrame)
chosenplayer2.Character:PivotTo(clonemap.Teleport2.CFrame)
for i = 10, 0, -1 do
status.Value = "Liquidation:"..i
wait(1)
end
clonemap:Destroy()
chosenplayer1.Character:PivotTo(lobby.Tp1.CFrame)
chosenplayer2.Character:PivotTo(lobby.Tp2.CFrame)
end
I’d recommend just making a variable named “Players” that is equal to :GetPlayers() so you don’t have to get the players multiple times, also use task.wait() not wait()
local lobby = game.Workspace.Spawn
local map = game.ReplicatedStorage.Maps:GetChildren()
local status = game.ReplicatedStorage.Status
local players = game.Players:GetPlayers()
while true do
for i = 5, 0, -1 do
status.Value = "Intermission:"..i
wait(1)
end
local chosenmap = map[math.random(1, #map)]
local clonemap = chosenmap:Clone()
clonemap.Parent = game.Workspace
status.Value = "Map:"..clonemap.Name
wait(2.5)
local chosenplayer1 = players[math.random(1, #players)]
local chosenplayer2 = nil
repeat
wait()
chosenplayer2 = players[math.random(1, #players)]
until chosenplayer2 ~= chosenplayer1
chosenplayer1.Character:PivotTo(clonemap.Teleport1.CFrame)
chosenplayer2.Character:PivotTo(clonemap.Teleport2.CFrame)
for i = 10, 0, -1 do
status.Value = "Liquidation:"..i
wait(1)
end
clonemap:Destroy()
chosenplayer1.Character:PivotTo(lobby.Tp1.CFrame)
chosenplayer2.Character:PivotTo(lobby.Tp2.CFrame)
end
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local lobby = game.Workspace.Spawn
local maps = ReplicatedStorage.Maps:GetChildren()
local status = ReplicatedStorage.Status
while true do
local players = Players:GetPlayers()
if #players >= 2 then
for i = 5, 0, -1 do
status.Value = "Intermission:" .. i
wait(1)
end
local chosenmap = maps[math.random(1, #maps)]
local clonemap = chosenmap:Clone()
clonemap.Parent = workspace
status.Value = "Map:" .. clonemap.Name
wait(2.5)
local chosenplayer1 = players[math.random(1, #players)]
local chosenplayer2 = nil
repeat
wait()
chosenplayer2 = players[math.random(1, #players)]
until chosenplayer2 ~= chosenplayer1
chosenplayer1.Character:SetPrimaryPartCFrame(clonemap.Teleport1.CFrame)
chosenplayer2.Character:SetPrimaryPartCFrame(clonemap.Teleport2.CFrame)
for i = 10, 0, -1 do
status.Value = "Liquidation:" .. i
wait(1)
end
clonemap:Destroy()
chosenplayer1.Character:SetPrimaryPartCFrame(lobby.Tp1.CFrame)
chosenplayer2.Character:SetPrimaryPartCFrame(lobby.Tp2.CFrame)
else
print("to start need atleast 2 players")
wait(5)
end
end
For get a random player you need atleast 2 players in server, try this
based on some small searches, I think that task.wait() is more accurate and doesn’t throttle your code, which wait() does, and task.wait() schedules the thread to be resumed after some time has elapsed without throttling. It also says that task.wait() is more efficient.
So task.wait() is just an improved version of wait() that is more efficient and accurate.
your code are fine but the issue here is math.random() function it will count argument #2 as empty if it’s less than first argument you can test this by simply try math.random(100,99) in command line [this mean that server were created but player don’t connected fast enough resulting in 0 player present at the time of script running] so the solution might be as @iiZanta_S said . Don’t go in execute loop until 2 players present in server or find workaround to wait until at least 1 player joined like adding if #game:GetService("Players"):GetPlayers() <= 0 then game:GetService("Players").PlayerAdded:Wait() end at the top of script