Problem with random position?

Hello again!

Sorry this is another post but i want to figure out why the objects that i spawn in won’t spawn mostly to the left of the map or baseplate.

Here is a piece of code i took from the script, i need help with figuring out how to make the objects spawn randomly from left to right or right to left, you get the point… :sweat_smile:

	stone.Position = Vector3.new(math.random(-114, 12),7.5,math.random(-124, 33))

You can see where i want the objects to spawn, PSpawn means “Player spawn” and obj means “objects”, sorry, im not good at “drawing” words…

Anyone know how to fix this?

Cheers! :wink:

I had the same problem. A solution was to remove the Vector3.new(). What’s left would be just the position. If you have any other questions, just ask. Hope this helps! :wink:

Thanks, but i do have a question, it gives me an error when i remove the Vector3.new, i’m very confused…

Try making the numbers in the math.random() only positive. Then multiply it by -1

Also replace the Vector3.new() with CFrame.new() and Stone.PositionStone.CFrame. Then add a Vector3.new() in the CFrame

Stone.CFrame = CFrame.new(Stone.Position + Vector3.new(math.random(1,126)*-1,0,math.random(1,157)*-1)

This should work better.

1 Like

Thank you so much, i’d wanna hear how this works though!

1 Like

Oh how nice. Well here how it goes.

CFrame and Position Technically can used interchangeably. However CFrame is it’s own property while Position is a property of Vector3

CFrame stands for Coordinate Frame, and it measure the parts Position in the world and its orientation.

in the script, we are grabbing the CFrame of the ‘stone’ and adding a new CFrame for the other obstacles to spawn, in reference to the stone’s original CFrame.

Stone.CFrame = CFrame.new()

Of course, it will not work only with the Vector3.new() since we need to specify what the Vector3 is adding too, so we add in the Position of the stone.

Stone.CFrame = CFrame.new(Stone.Position + Vector3.new())
-- Note the Stone.CFrame + Vector3.new() is not valid, since they're different properties.

What is inside the Vector3.new() is the element of randomness you added before. nothing new there.

And thats how it works. It’s recommended to move the CFrame of anything, instead of using Position since CFrame conserve’s the Part, or models orientation. Add can even change it, without having to add another line below changing the Orientation, like so:

Part.CFrame = CFrame.new(Part.Position) * CFrame.Angles(math.rad(90),0,0)
--CFrame.Angles deals with the orientation. Very simple to use too.