erverScriptService.Script:14: invalid argument #2 to 'random' (interval is empty)

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

there is an error in this line

local chosenplayer1 = game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())]

Print #game.Players:GetPlayers()

1 Like

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()

1 Like

and what is the difference between wait() and task.wait()

still not working. the same mistake

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

What does #players print?
ch

1 Like

Some scripts/anti-cheats change service names in order to fix it, here is how I would get these services using GetService instead:

local chosenplayer1 = game:GetService("Players"):GetPlayers()[math.random(1, #game:GetService("Players"):GetPlayers())]
1 Like
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

2 Likes

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.

1 Like

actually doesn’t matter task.wait() or wait() in this code they both will work as the same

1 Like

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

image

1 Like

Thank you for explaining better than me you got the point, we need to let him to test

1 Like
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.