Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
walvie 12
5 years ago

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.

0
The error answers your problem: You can't perform math on a table. DeceptiveCaster 3761 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

1
None of this answer explains what the error is in the code. This is all irrelevant :/ User#5423 17 — 5y
0
^ DeceptiveCaster 3761 — 5y
1
Why are you using `math.huge`? ScriptGuider 5640 — 5y
0
Je-sus Christ what a "swing and a miss" answer DeceptiveCaster 3761 — 5y
View all comments (3 more)
0
Nice "bad argument #2 to 'random' (interval is empty)" User#24403 69 — 5y
0
and math.huge isn't the same as infinity, it is the 64 bit integer limit theking48989987 2147 — 5y
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 SerpentineKing 3885 — 5y
Ad
Log in to vote
0
Answered by
ben0h555 417 Moderation Voter
5 years ago

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))
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
Random is not uppercase, it's lowercase. Try again. DeceptiveCaster 3761 — 5y
0
Sorry, Studio usually auto-corrects me in that kinda stuff.. User#26817 0 — 5y

Answer this question