Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Selecting random player, error, why?

Hi guys, I'm TheLorizz and I don't know why but the math.randoms I put doesn't work, why? This is the first time! I need to select a traitor and a detective from the innocents array, but the output gives me this on "math.random()" lines: bad argument #2 to 'random' (interval is empty)

This is the code:

local players = game.Players:GetChildren()
local innocents = {}
local traitor
local detective

function findType(p)
	local td = p:FindFirstChild("TempData")
	local type = td:FindFirstChild("Type")
	return type
end

_G.startGame = function()
	local h = Instance.new("Hint", game.Workspace)
	h.Text = "Selecting traitor!"
	wait(2)
	for i, player in pairs (players) do
		table.insert(innocents, player)
		local type = findType(player)
		type.Value = "Innocent"
		wait()
	end
	wait(1)
	traitor = innocents[math.random(1, #innocents)]
	local ttype = findType(traitor)
	ttype.Value = "Traitor"
	table.remove(innocents, traitor)
	h.Text = "Traitor selected:" .. " " .. traitor
	wait(1)
	h.Text = "Selecting detective!"
	detective = innocents[math.random(1, #innocents)]
	local dtype = findType(detective)
	dtype.Value = "Detective"
	table.remove(innocents, detective)
	h.Text = "Detective selected:" .. " " .. detective
	wait(1)
	h.Text = "Game started!"
end

Why? I repeat, this is the first time!

1 answer

✓ Accepted answer

That happens when you have empty tables. Why that happens? Essentially this is what happens: math.random(1, 0) and Lua doesn't like that. Make sure the tables aren't empty.

1 comment

0

local players = game.Players:GetChildren() that's why! I don't update this variable, THANKS!

This discussion is preserved as it was. New answers, comments, and votes are closed.