I’m trying to make objects spawn randomly, but it is not being very random. While some objects do indeed spawn randomly, other objects usually spawn at the same location and then start stacking. This is my code:
function newObj(area, areaNum)
local r = math.random(30, 75)
local spawner = workspace[area]
local obj = objectsArea1:FindFirstChild(area1obj[math.random(1, #area1obj)]):Clone()
local x,z = spawner.Position.X,spawner.Position.Z
x,z = math.random(x - r,x + r),math.random(z - r,z + r)
local pos = Vector3.new(x,spawner.Position.Y,z)
obj.Parent = workspace.Objects["Area" .. areaNum]
obj:MoveTo(pos)
end
It could be because you’re using :MoveTo which accounts for part collision, your parts are some how colliding.
Try :SetPrimaryPartCFrame and make sure you have a PrimaryPart set.
If that does not work I am not sure because I used your code (in a slightly edited form) and it worked for me.
function newObj(spawner)
local r = math.random(30, 75)
local obj = Instance.new("Model")
local temp = Instance.new("Part")
local x,z = spawner.Position.X,spawner.Position.Z
x,z = math.random(x - r,x + r),math.random(z - r,z + r)
local pos = CFrame.new(x,spawner.Position.Y,z)
temp.Anchored = true
temp.Parent = obj
obj.PrimaryPart = temp
obj:SetPrimaryPartCFrame(pos)
obj.Parent = workspace
end
for i = 1, 10 do
newObj(workspace:WaitForChild("A"))
end
From my speculation, it looks like some other aspect of your code might be throwing an error, causing your function to never move the object to a new location. Why do I say that? Because one thing that jumps out at me is the fact I can’t see any green tags above those objects stacked on one another in the picture, not only that, but I also noticed you don’t actually create any new objects from the code you shared, but rather, you’re attempting to search for a part and then move it.
Check the output logs next time this happens. Do you see any red text?
And for feature reference upon editing the code to use :MoveTo I noticed the model was not moving!
Here is the edited-edited code which is my code edited to use :MoveTo which is just your code basically.
Click to see code
function newObj(spawner)
local r = math.random(30, 75)
local obj = Instance.new("Model")
local temp = Instance.new("Part")
local x,z = spawner.Position.X,spawner.Position.Z
x,z = math.random(x - r,x + r),math.random(z - r,z + r)
local pos = CFrame.new(x,spawner.Position.Y,z)
temp.Anchored = true
temp.Parent = obj
obj.PrimaryPart = temp
obj:MoveTo(pos.Position)
obj.Parent = workspace
end
for i = 1, 10 do
newObj(workspace:WaitForChild("A"))
end
However this issue does not occur if I set the parent before calling :MoveTo but you are already doing that. I am not particularly sure what the issue is but it appears to be something with :MoveTo. Using :SetPrimaryPartCFrame worked for me so maybe you should try it?
I used :SetPrimaryPartCFrame() and now the collision thing isn’t happening anymore, but it is still going to the same position. I used your example, (@EpicMetatableMoment sorry meant to reply to you) here is my code (I also printed the positions).
function newObj(area, areaNum)
local r = math.random(30, 75)
local spawner = workspace[area]
local obj = objectsArea1:FindFirstChild(area1obj[math.random(1, #area1obj)]):Clone()
local x,z = spawner.Position.X, spawner.Position.Z
x,z = math.random(x - r,x + r), math.random(z - r,z + r)
local pos = CFrame.new(x,spawner.Position.Y,z)
print(pos)
obj:SetPrimaryPartCFrame(pos)
obj.Parent = workspace.Objects["Area" .. areaNum]
end
for i = 10, 0, -1 do
newObj("SpawnArea1", 1)
end
^^ As you can see the same position is getting printed out 10 times. I don’t understand this, it’s really confusing
EDIT: If you want to use math .random, then try doing
x,z = math.random(math.floor((x - r)*100),math.floor((x + r)*100)), math.random(math.floor((z - r)*100),math.floor((x - r)*100)) and local pos = CFrame.new(x/100,spawner.Position.Y,z/100).
This will keep accuracy to 2 decimal places, you can also use 1000 (since position has up to 3 decimal places) but that seems a bit excessive. @Rezault