How to position a part randomly on a part?

So what im trying to do is place a ore rock randomly on the base of the map but sometimes it will put itself completely off the part.

local pos = Vector3.new(math.random(-base.Size.x/2, base.Size.x/2), 2, math.random(-base.Size.z/2,base.Size.z/2))
rock:SetPrimaryPartCFrame(CFrame.new(pos.x,pos.y,pos.z))

3 Likes

Do you wish that the rock copies the rotation of the base you are placing it on? Cause that is what this will do:

rock:SetPrimaryPartCFrame(base.CFrame * CFrame.new(pos))

This code treats pos as if its in the local space of the base you are trying to place it on.

2 Likes

Perhaps because you’re using math.random? It’s kinda old and I recommend using the new Random method.

Example

local newrandom = Random.new() 
local randomnumber = newrandom:NextNumber(0, 10) 
3 Likes

this will keep the rock on the part, my code sometimes puts it off the part…

Is the base part centered at the origin? If it’s not you’ll also need to add in it’s position to your random position.

Also, you might want to leave a little margin at the edges so the ore won’t spawn hanging off the edge of the part.

2 Likes

already done and it still goes off.

Don’t you want the rock to stay on the part?
this is the result of using this code, with the baseplate as the base and a random stack of 3 blocks being the rock.

local m = Instance.new("Model")
m.Parent = workspace
for k = 1, 50 do
	local base = workspace.Baseplate
	local rock = workspace.Model:Clone()
	local pos = Vector3.new(math.random(-base.Size.x/2, base.Size.x/2), base.Size.y/2, math.random(-base.Size.z/2,base.Size.z/2))
	rock:SetPrimaryPartCFrame(base.CFrame * CFrame.new(pos))
	rock.Parent = m
end

also if the basepart is rotated it will still work:

3 Likes

did not work…

helppls

as you can see the rock is going off the part…

Code:

local pos = Vector3.new(math.random(-base.Size.x/2, base.Size.x/2), 2, math.random(-base.Size.z/2,base.Size.z/2))
rock:SetPrimaryPartCFrame(base.CFrame * CFrame.new(pos))

Does your base have a BlockMesh inside it that makes the base appear smaller? also which part on the rock is the PrimaryPart?

1: No it does not

2: the primary part is the rock mesh.

what does the rock mesh look like when selected? like the bounding box.

rock

thats the selection.

I do think this may be caused as you are placing the models relative to (0,0,0) not relative to the base position, so

SetPrimartPartCFrame(CFrame.new(base.Position + pos))

wow

still goes off

I am 98% sure that that should work…

Can you try printing

  1. the base position
  2. the random position and
  3. the combined positions

1: 309.975342, 699.647339, -16.3491192
2: 197, 2, 99
3: 506.975342, 701.647339, 82.6508789

Whats the base size?

well its kinda random, it chooses a random base and gives it a random pos on that base…

Does it use the base position before moving or after?
And can you give me the 3 numbers again but with the base pos aswell so i can see if any of the numbers dont match