Math.random interval is empty

I have a problem were the race intermission start and every player gets teleported to their place. The thing is that the behavior is what I expect but I get this error " Workspace.Curse.Main Race.RaceScript:262: invalid argument #2 to ‘random’ (interval is empty)"

RaceManager.IntermissionStarted:connect(function()

	local cars = script.Parent.Masini:GetChildren()

	for x, platform in ipairs(script.Parent.Pozitii:GetChildren()) do

		local randomNum = math.random(1, #cars)
		local randomCar = cars[randomNum]
		table.remove(cars, randomNum)

		randomCar:SetPrimaryPartCFrame(CFrame.new(platform.Position + Vector3.new(0,0.5,0)) * CFrame.Angles(math.rad(0),0,math.rad(0)))



	end

	local intermissionDuration = RaceManager:GetIntermissionDuration()

	pushNotification("Race starting Soon!", intermissionDuration ) 
end)

Apparently #cars doesn’t exist. I had a similar issue recently, but that was with a dictionary, which :GetChildren() shouldn’t be returning. It should work with tables.

Try the table.getn() function.

1 Like

Still the same "Workspace.Curse.Main Race.RaceScript:262: invalid argument #2 to ‘random’ (interval is empty) "

Could you confirm there are objects in the Masini object?

print(cars)

22
here

Looks like you have the legacy output, do it this way instead:

for _,v in pairs(cars) do
   print(v)
end

Going to put my penny in this, but from the script, cars is this:

local cars = script.Parent.Masini:GetChildren()

But what you’re iterating through below is this:

script.Parent.Pozitii:GetChildren()

They’re two different collections of objects, so they could be different in size. I don’t suppose there’s less children in ‘script.Parent.Masini:GetChildren()’ than there are in ‘script.Parent.Pozitii:GetChildren()’? Example could be (replacing ‘#cars’ with a static number for this example:

local randomNum = math.random(1, 0)

How could this work, since the first argument needs to be lower than the second. Something like this might be happening possibly.

They’re in different numbers. Because it may not always be the same number of players. There can be 5 players and the race has 10 spots available and I can’t let those 5 players wait after another 5 people and the race can start if it’s from one player up

Don’t suppose you’d be able to change your script to this like so?

RaceManager.IntermissionStarted:connect(function()
	local cars = script.Parent.Masini:GetChildren()
	local platforms = script.Parent.Pozitii:GetChildren()

	-- Adding two new prints here for testing.
	print("Car count: " .. #cars)
	print ("Platform count: " .. #platforms)
	
	for x, platform in ipairs(platforms) do
		--[[ Random.new():NextInteger() uses a completely different randomseed, 
		using random.new() directly uses the same seed, so you'd get the 
		same number pattern. ]]--
		local randomNum = Random.new():NextInteger(1, #cars) 

		local randomCar = cars[randomNum]
		table.remove(cars, randomNum)

		randomCar:SetPrimaryPartCFrame(CFrame.new(platform.Position + Vector3.new(0,0.5,0)) * CFrame.Angles(math.rad(0),0,math.rad(0)))
	end

	local intermissionDuration = RaceManager:GetIntermissionDuration()
	pushNotification("Race starting Soon!", intermissionDuration ) 
end)

I’ve replaced the random.new() with Random.new():NextInteger. This way, the seed that Random.new() uses is completely different every time the game is initialised. If you were to remain using random.new(), the seed would be the same, so you’d get the same number pattern every time you started the game.

Other than that, I’ve added some prints for debugging, I don’t suppose you’ll be able to use this and show me the output/console after please? :slight_smile:


The idea is that that loop happens 2 times. I will have to look in RaceModul to see when that event fires 2 times. The car is where it needs to be

The intermission event is fireing only one time

Well that’s the issue then. It’s because you’re iterating through platforms, which has 6 children objects, while cars only has 1. So on the second iteration, #cars would return 0, since there’s no cars in the array anymore. What you’ll have to do is break out of the loop the moment there’s no cars left to be removed.

RaceManager.IntermissionStarted:connect(function()
	local cars = script.Parent.Masini:GetChildren()
	local platforms = script.Parent.Pozitii:GetChildren()

	-- Adding two new prints here for testing.
	print("Car count: " .. #cars)
	print ("Platform count: " .. #platforms)
	
	for x, platform in ipairs(platforms) do
		-- When there's no more cars, break out of the loop.
		if (#cars == 0) then
			break
		end
		
		--[[ Random.new():NextInteger() uses a completely different randomseed, 
		using random.new() directly uses the same seed, so you'd get the 
		same number pattern. ]]--
		local randomNum = Random.new():NextInteger(1, #cars) 

		local randomCar = cars[randomNum]
		table.remove(cars, randomNum)

		randomCar:SetPrimaryPartCFrame(CFrame.new(platform.Position + Vector3.new(0,0.5,0)) * CFrame.Angles(math.rad(0),0,math.rad(0)))
	end

	local intermissionDuration = RaceManager:GetIntermissionDuration()
	pushNotification("Race starting Soon!", intermissionDuration ) 
end)
1 Like

This would mean there’s unoccupied platforms, but I’m not sure if this is okay depending on the way your game is going to be designed etc. If you need the same amount of platforms as there are players, you could create them on the fly the moment a player joins using the .PlayerAdded event.

1 Like