Help with math.random?

Hello! This is my first post ever!

Anyways, i need a lot of help with math.random, im still learning scripting and making a few test games!
Im making a game where you press this button that spawns in random things.
There are a few events that i want to have a random postition when spawned in(moved from server storage to workspace)

Here is the line of code that i need help with:

   local wall = game.ServerStorage.Wall:Clone()

   wall.Parent = workspace
   **wall.Position = Vector3.new(math.random(12.2, -122.4),15.1,math.random(12.2, -122.4))**
   wait(timer)
   wall:Destroy()

The line of code with the stars is the one code that is giving me this error message in the output:
Workspace.Button.HitBox.SpawnScript:52: invalid argument #2 to ‘random’ (interval is empty)

Anybody who knows what to do, i would be glad to listen to!

Cheers! :wink:

Try to swap the order of the arguments and see if that helps.

i.e. math.random(-122.4, 12.2)

1 Like

It worked! Thanks a lot Tyler148!

Awesome, just wanted to make sure that would work before explaining why.

The first argument in math.random() has to be less than the second. You are creating a range of numbers to generate from. With that said, -122.4 < 12.2 so the arguments should be (-122.4, 12.2).

Mark as solution if you found it helpful, have a nice day.

math.random()

Doesn’t work with decimals, instead use

Random.new():NextNumber(-122.4, 12.2)

Works exactly the same, but it includes decimal points.

2 Likes