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

How do you make a part spawn randomly in a certain area?

I am trying to make a script witch a part will spawn randomly in a restricted area but i can't seem to do it.

points.Position = Vector3.new(Random<87, 1.5, Random<133)

When the script runs it gives me the following error:

Workspace.Script:3: attempt to compare table with number

Thank you in advance.

1 comment

0

The error answers your problem: You can't perform math on a table.

3 answers

Random returns an object as specified here: https://devforum.roblox.com/t/a-random-feature/64517

You can use math.random() instead to generate the values you want, it takes a random value from the min parameter to the max parameter and returns it:

Revised Code

local random1 = math.random(-math.huge, 87)
local random2 = math.random(-math.huge, 133)
points.Position = Vector3.new(random1, 1.5, random2)

math.huge is the same as infinity

7 comments

1

None of this answer explains what the error is in the code. This is all irrelevant :/

0

^

1

Why are you using math.huge?

0

Je-sus Christ what a "swing and a miss" answer

0

Nice "bad argument #2 to 'random' (interval is empty)"

0

and math.huge isn't the same as infinity, it is the 64 bit integer limit

0

i tested and math.huge worked for me so idk, and alright @theking, but i'll just call it inf since that's essentially how it functions when I use it. @ScriptGuider, I'm using math.huge since he wants any random value below 87 or 133

I suggest using math.random, you cant perform math on a table, so instead you use the math.random function to satisfy the need to randomize a number.

points.Position = Vector3.new(math.random(0,87), 1.5, math.random(0,133))

You need to set variables for X and Z, which are math.Random. Math.Random will find a random number between two numbers. Looking at the value of x, it will find a random value between 0 and 87.

local x = math.random(0,87)
local y = 1.5
local z = math.random(0,133)

points.Position = Vector3.new(x,y,z)

I am still learning so if I'm wrong then sorry.

2 comments

0

Random is not uppercase, it's lowercase. Try again.

0

Sorry, Studio usually auto-corrects me in that kinda stuff..

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