OutlookG
(Outlook)
May 21, 2018, 5:16pm
#1
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
Baumz
(Baumz)
May 21, 2018, 5:32pm
#2
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
Tom_atoes
(Tom_atoes)
May 21, 2018, 5:34pm
#3
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
OutlookG
(Outlook)
May 21, 2018, 5:47pm
#4
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
OutlookG
(Outlook)
May 21, 2018, 5:52pm
#6
already done and it still goes off.
Baumz
(Baumz)
May 21, 2018, 6:00pm
#7
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
OutlookG
(Outlook)
May 21, 2018, 6:04pm
#8
did not work…
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))
Baumz
(Baumz)
May 21, 2018, 6:07pm
#9
Does your base have a BlockMesh inside it that makes the base appear smaller? also which part on the rock is the PrimaryPart?
OutlookG
(Outlook)
May 21, 2018, 6:08pm
#10
1: No it does not
2: the primary part is the rock mesh.
Baumz
(Baumz)
May 21, 2018, 6:09pm
#11
what does the rock mesh look like when selected? like the bounding box.
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))
I am 98% sure that that should work…
Can you try printing
the base position
the random position and
the combined positions
OutlookG
(Outlook)
May 21, 2018, 8:09pm
#17
1: 309.975342, 699.647339, -16.3491192
2: 197, 2, 99
3: 506.975342, 701.647339, 82.6508789
OutlookG
(Outlook)
May 21, 2018, 9:39pm
#19
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