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

invalid argument #2 to 'random' (interval is empty)?

The Code is:

while true do
	
	local players = game.Players:GetChildren()
	

	
	local giant = players[math.random(1, #players)]
	
	wait()
	
	for i = 1,20 do
		
		local player = game.Players:GetChildren()
		status.Value = "Intermision "..20-i
		wait(1)
	end

	for i = 1,#players do
		if players[i].Character ~= nil then
			local spawnLocation = math.random(1,#workspace.Teleports:GetChildren())
			local giantspawnLocation = math.random(1,#workspace.GiantTeleport:GetChildren())
			players[i].Character:MoveTo(workspace.Teleports:GetChildren()[spawnLocation].Position)
			giant.Character:MoveTo(workspace.GiantTeleport:GetChildren()[giantspawnLocation].Position)
			PlayerSize(giant)

		end
	end
	for s = 1,20 do
		status.Value = "Game "..20-s
		wait(1)
		for l = 1,#players do
			
			giant.Character.HumanoidRootPart.Touched:Connect(function(player)
				local humanoid = players[l].Character.Humanoid
				if humanoid.Parent.Name ~= giant.Name then
					humanoid:TakeDamage(100)
				end
			end)
		end
	end

	print(giant)

	wait(1)
	for p = 1,#players do
		

		giant.Character.Humanoid:TakeDamage(1000)
		players[p].Character.Humanoid.Health = 0
	end

end

1 comment

0

is that all ur code is? if the variable players is 0, this error should happen. https://scriptinghelpers.org/lua#A4JwlgdgLgFAtgQygCwHQgRAJgezjARgBoAGASjKA

1 answer

✓ Accepted answer

Sometimes Roblox error codes are pretty unintuitive, this happens to be one of those cases.

invalid argument #2 to 'random' (interval is empty)

math.random spits out this error when the second value passed is smaller then the first.

print(math.random(1,5))
-- works fine

print(math.random(5,1))
-- causes error

My guess is that you are executing this code before any players have the chance to load. Thus making the players table empty and it registering as less then one.

Try delaying the code, using PlayerAdded or implementing a check for whether there are players in the server.

1 comment

0

thank you!

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