Random spawning not random

Hello, developers.

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

Wanted result:

Actual result:
image

Thank you! :slight_smile:

2 Likes

Try assigning x and z separately instead of x,z.

3 Likes

Did not work, still got the same result.

Are you getting any errors? Also there’s no reason to do local pos = Vector3.new, you can just do obj:MoveTo(Vector3.new())

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

5 Likes

It accounts for collision by putting the model above the collision(s). That doesn’t mean it’ll not move at all.

1 Like

Could you please show me the modified code?

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?

1 Like

No errors; The green tags didn’t show because I was too far away from the objects, and I am cloning each object from ServerStorage.

1 Like

Edited my post.

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?

try putting the .Parent = workspace line above the MoveTo()

He already does that in his code.

1 Like

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

image

^^ As you can see the same position is getting printed out 10 times. I don’t understand this, it’s really confusing :confused:

Mind printing the results of r, x and z every iteration?

local x,z = spawner.Position.X,  spawner.Position.Z
x,z = math.random(x - r,x + r), math.random(z - r,z + r)
print(x, z, r)

It is always printing the same thing: -31 572 33

image

I feel like this is an issue with roblox’s math.random().

Try this:
(don’t think this will work but who knows /shrug)
x, z = math.random(r - x, r + x), math.random(r - z, r + z)

Alternatively try changing the seed.
math.randomseed(tick())

it seems math.random only takes integer arguments. Try Random | Documentation - Roblox Creator Hub or floor your position/and other stuff to make it an int.

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

1 Like

All that did is give an error. Changing seed also did not work.

image

That gave the same error as above.

I ran this exact code, swapping out obj for a new model with a single part in it, and got 10 unique positions.

Here’s the exact code I ran: Demo.rbxl (20.0 KB) . A couple lines are edited due to the variables that were missing from the code you provided.

It may be worth you including the whole script as I can’t seem to find any issue with the code you posted.