How to make a random part generate in a random spot

About

Hello! I have a problem with my script, like almost everyone posting in this section. :sweat_smile: Keep In mind I am pretty new to intermediate scripting. So I am making a simulator and I want the object that you want to collect to fall from the sky at random spots. I went and looked on scripting helpers and found some parts of the script from there.

My Code

local spawnPart = script.Parent
local standardSnowCone = game.ServerStorage.SnowCones:WaitForChild("SnowConeL1")


while true do
	local waitTime = math.random(1,4)
	wait(waitTime)
	local clonedSnowCone = standardSnowCone:Clone()
	clonedSnowCone.Parent = workspace
	clonedSnowCone.CFrame = CFrame.new(Vector3.new(math.random(-27.574,-160.247),math.random(70.992,72.276),math.random(199.392,-11.186)))

	
end

The Errors
#1
08:27:52.578 - Workspace.SpawnPart.Script:10: bad argument #2 (interval is empty)

#2 (Says source isn’t available might not even have anything to do with this script :confused: )
08:27:51.371 - Connect is not a valid member of Player

This has been answered before:

2 Likes

I took a quick look and this is what I see from first impressions.

Error 1

math.random(x,y)

Only works where x<=y , so your first and third math.random will not work. since x>=y in those cases.

Why?

Inputs to math.random are checked using the following code.

case 2: {  /* lower and upper limits */
int l = luaL_checkint(L, 1);
int u = luaL_checkint(L, 2);
luaL_argcheck(L, l<=u, 2, "interval is empty");
lua_pushnumber(L, floor(r*(u-l+1))+l);  /* int between `l' and `u' */
break;
}

-27.574 is not less than -160.247 so will return “interval is empty”

199.392 is not less than -11.186 so will return “interval is empty”

Error 2

Is not related to this code.

Hope this helps,
Allie

1 Like

The second error does not pertain to this excerpt because it does not mention Connect at all. The issue why the first error occurs is because of the order of the arguments you pass to math.random. This error occurs when the minimum value (first argument) is larger than the maximum (2nd one).

There are two cases why this error occurs:

math.random(-27.574,-160.247)

-27.574 > -160.247 (is greater)

math.random(199.392,-11.186)

199.392 > -11.186

Simply switch the order of the arguments for those two functions and it’ll work as intended

1 Like

Also, there is no need to use decimal point when using math.random(n, m) because the arguments will be considered integers:

  • math.random(1, 2) will always return 1 or 2

  • math.random(1.05421, 2.85677) will always return 1 or 2 too

1 Like