Random spawning not random

I checked the demo and I inserted it into my own game and it seems to work, here’s my script:

wait(1)

local objects = game:GetService("ServerStorage").Objects
local objectsArea1 = objects.Area1

local area1obj = {}
for i,v in pairs(objectsArea1:GetChildren()) do
	table.insert(area1obj, v.Name)
end

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.Parent = workspace.Objects["Area" .. areaNum]
	obj:SetPrimaryPartCFrame(pos)
end

for i = 10, 0, -1 do
	newObj("SpawnArea1", 1)
end

workspace.Objects.Area1.ChildRemoved:Connect(function()
	newObj("SpawnArea1", 1)
end)

Honestly, I have no idea what is happening. I tried cloning a random item or doing :FindFirstChild() to find a specific item, and I’ve been getting the same results. The only time I haven’t been getting the same results is when I did Instance.new().

image

It’s just this over and over again…

EDIT: I have a model with multiple parts welded together. Idk if this would affect the results, but I don’t think it should

I don’t know what I did, but all of a sudden it started working. Thank you all for your contributions!

I edited your script, I have not tested it:



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)
    local CanPut = true
    for _,Child in ipairs(workspace.Objects["Area" .. areaNum]:GetChildren()) do
           if not Child.Position = pos then
                  CanPut = true
                  break
                  else
                  CanPut = false
                  break
           end
    end
    if CanPut then
	obj.Parent = workspace.Objects["Area" .. areaNum]
	obj:MoveTo(CFrame.new(pos))
    else
     newObj(area, areaNum)
    end
end

This script does the exact thing you do, but it put it 100% Random and it doesn’t put one object over other. If an error appears, tell me