My Models aren't spawning in a zone

So basically my models clone don’t spawn in a designated zone instead they spawn outside of it

Picture:

Full Module
local Bugss = {}

Bugss.AllBugs = game.ServerStorage.Bugs:GetChildren()
Bugss.BugZone = game.Workspace.BugZone
Bugss.BugLimit = Bugss.BugZone.BugLimit
local limit = 0
function Bugss:SpawnBug()
	if #Bugss.AllBugs > 0 then
        while wait() do
			local Random = math.random(1, #Bugss.AllBugs)
			local RandomBug = Bugss.AllBugs[Random]
            
			local BugClone = RandomBug:Clone()
			BugClone.Parent = Bugss.BugZone
            
            

            local x = math.random(math.min(4, Bugss.BugZone.Position.X), math.max(4, Bugss.BugZone.Position.X))
            local y = Bugss.BugZone.Position.Y +3
            local z = math.random(math.min(4, Bugss.BugZone.Position.Z), math.max(4, Bugss.BugZone.Position.Z))
            
            print("XPos: "..x,"YPos: " ..z)
            BugClone:SetPrimaryPartCFrame(CFrame.lookAt(Vector3.new(x,y,z),Vector3.new(1,0,0)+Vector3.new(x,y,z),Vector3.new(1,0,0)))
            limit = limit +1
            print(limit)
            if limit >= Bugss.BugLimit.Value then
                break
            end
            
			wait(1)
		end
	end

end

return Bugss
Part that's Stumping me
            local x = math.random(math.min(4, Bugss.BugZone.Position.X), math.max(4, Bugss.BugZone.Position.X))
            local y = Bugss.BugZone.Position.Y +3
            local z = math.random(math.min(4, Bugss.BugZone.Position.Z), math.max(4, Bugss.BugZone.Position.Z))
            
            print("XPos: "..x,"YPos: " ..z)
            BugClone:SetPrimaryPartCFrame(CFrame.lookAt(Vector3.new(x,y,z),Vector3.new(1,0,0)+Vector3.new(x,y,z),Vector3.new(1,0,0)))

Why don’t you check after if the part is touching the part / region? Then you could try again I guess.

maybe try:

local rx,rz = math.random(-Bugss.BugZone.Size.X,Bugss.BugZone.Size.X),math.random(-Bugss.BugZone.Size.Z,Bugss.BugZone.Size.Z)
local randomposition = Bugss.BugZone.Position+Vector3.new(rx,0,rz)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.