Invalid argument #1 to 'random' (interval is empty)

Whenever this code runs, after a while it returns an error saying: Workspace.cogworld.bomb rain:4: invalid argument #1 to ‘random’ (interval is empty)

here’s my code:

while true do --Loops the script
	wait(1) 
	local children = script.Parent.bombs:GetChildren()
	local M = math.random(#children)
	droplet = children[M]:Clone()
	droplet.Parent = game.Workspace
	droplet.Anchored = false
	droplet.Transparency = 0
end
1 Like

At this line, when there are no children present in the bombs object, its going to return nil, and it will be the value of the variable children, then when you call math.random(), it expects a numerical value, which nil isn’t, so do this to make your code safe:-

while true do --Loops the script
	wait(1) 
	local children = script.Parent.bombs:GetChildren()
    if(children == nil) then break end --this will terminate your loop, add some other code if you wanna achieve something else.
	local M = math.random(#children)
	droplet = children[M]:Clone()
	droplet.Parent = game.Workspace
	droplet.Anchored = false
	droplet.Transparency = 0
end
2 Likes

wait doesnt math.random need 2 arguments??

2nd is optional. it is not necessary to be provided.

1 Like

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