ServerScriptService.Script:3: invalid argument #2 to 'random' (interval is empty) Help, please

This is supposed to be a script that randomly clones a model and puts them on a random z postion, but i get this error “ServerScriptService.Script:3: invalid argument #2 to ‘random’ (interval is empty)” im not very good with lua i know the basics, and other stuff.

for i = 1, 50 do
	local rr = math.random(-47.75, -3993.75)
	local f = game.ServerStorage.Folder.Mo:Clone()
	f.Parent = game.Workspace
	f.PrimaryPart.Position = Vector3.new(44, 5.25, rr)

end

I would be grateful for any help!

you should swap the order of the bounds so that the lower bound is less than the upper bound

upper bound (-3993.75) is less than the lower bound (-47.75).

for i = 1, 50 do
	local rr = math.random(-3993.75, -47.75)
	local f = game.ServerStorage.Folder.Mo:Clone()
	f.Parent = game.Workspace
	f.PrimaryPart.Position = Vector3.new(44, 5.25, rr)
end
1 Like

Ye that fixed that problem but now i got a new error
ServerScriptService.Script:5: attempt to index nil with ‘Position’

to me it looks like PrimaryPart is nil, which means it is not referencing a valid object

Does “Mo” have a PrimaryPart? & make sure its a descendant of the model

image
Here the image of the model, if it helps

May i ask what is your PrimaryPart? what is the main part that you wanna move.

It is located next to the PrimaryPart here on your model

image

oh ye thers no primarypart
image

In that case you will have to change the script so that it actually detects the part that you wanna move, which is?

i believe its the Frame model am i right?

Could i union the parts?
and use the union as a primary part?

Yes, that is what i would do :

Ungroup “Frames”, Union it (if its possible for you)

Rename it “Frames” & I will help you change the script after you’ve done that

alr so i unioned the frames but left the touch part is that okay?
image

Yes aslong as the PrimaryPart on your model is Frames but, Could you click on the empty field of primary part and then when you see a red cross, click on the frames union so that it will make the primarypart “Frames”

image

image

You should get a black cursor with a part next to it

There i made it the primary part
image

for i = 1, 50 do
	local rr = math.random(-3993.75, -47.75)
	local f = game.ServerStorage.Folder.Mo:Clone()
	f.Parent = game.Workspace
	f.PrimaryPart.Position = Vector3.new(44, 5.25, rr)
end

now that should be it

The numbers must be whole numbers for reliable behavior. The script wouldn’t know the depth of the random number you want, so use whole numbers instead. You can multiply the range by a factor of 10 and divide it the same way after to make it generate random decimals.

print(math.random(1,10)) --> 1,2,3,4, ...
print(math.random(1*100,10*100)/100) --> 1.22,4.56,2.92, ...
math.random(-39.9375 * 100, -0.4775 * 100) / 100

@Yourrichbacon

Thanks with a bit of tweaking/ 1 line i made so the touch part also is in the right place,

for i = 1, 50 do
	local rr = math.random(-3993.75, -47.75)
	local f = game.ServerStorage.Folder.Mo:Clone()
	f.Parent = game.Workspace
	f.PrimaryPart.Position = Vector3.new(44, 5.25, rr)
	f.Touch.Position = Vector3.new(44, 5.25, rr)
end

Thank you for the help, have a nice day!

1 Like

No problem! anytime and take in consideration what blueberry said.

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