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

math.random error. Please help?

I get this error on my main game script: 17:36:39.401 - ServerScriptService.MainGameScript:26: bad argument #2 to 'random' (interval is empty)

This is line 26: d1 = players[math.random(1, #players)]

Yes, players is defined. Please help? Confused. Also, I know that the second argument cannot be lower than the first one, but the second argument is always wrong even if the table is reading it, let's say 1-3.

2 comments

0

Please show us how [players] is defined in the script.

0

local players = game.Players:GetPlayers()

2 answers

Please make sure players is defined as such:

local players = game.Players:GetPlayers()

Make sure you're using the typical events to detect players being added to your game, before calling this - otherwise you won't get the correct results.

game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character)
	
	
	-- Your code goes here 
 	local players = game.Players:GetPlayers()
	--[[
		 this is for test only , but you would put the code below in a function that picks the random player before starting  a new round
	--]]	

	d1 = players[math.random(1, #players)]
	
end)
end)

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