Currently I’ve made a system where BaseParts (Mesh parts & Parts) randomly spawn on the map and I use GetTouchingParts() to check if they collided with each other. Currently I’m trying to do it with models, I’ve tried to loop through every object in the Model to check If It’s touching another part in another model yet it doesn’t work. Here is my current code:
local RandomChest = math.random(1, #ReplicatedStorage.Chests:GetChildren())
local ChestClone = ReplicatedStorage.Chests:GetChildren()[RandomChest]:Clone()
ChestClone.PrimaryPart = ChestClone["Connection"]
for _, Part in ipairs(ChestClone:GetChildren()) do
Part.Anchored = true
end
ChestClone.Parent = WorldMap.Chests
local Ground = WorldMap.Ground.Ground
local Height = 1.5
local RandomXPosition, RandomZPosition = module.GetRandomPosition(WorldMap, Ground, Height)
if RandomXPosition and RandomZPosition then
ChestClone:MoveTo(Ground.Position + Vector3.new(RandomXPosition, Height,RandomZPosition))
end
for _, Part in ipairs(ChestClone:GetChildren()) do
for _, GettingTouched in pairs(Part:GetTouchingParts()) do
pcall(function()
if (GettingTouched.Parent ~= Part.Parent) then
print(GettingTouched)
ChestClone:Destroy()
end
end)
end
end
Make it a model, but put 1 transparent CanCollide off box around it, the same size as the edges of the model.
Make that part the subject of the script, not all the other Parts.
The other thing you may want to do is place them on a grid type system and put the Position of each Model in a table. When randomly placing items you can check the table to see if an item is already at that stored Position. If the stored Position is there, then don’t place the new Model there.
Is it a fairly rectangular Model?
If so put a large Part that’s the same size as the Model’s bounding box (outline).
How does that defeat the purpose of them spawning randomly? If the Model is a fairly rectangular box then:
Place a model in a random Position.
Put it’s Position (and size so you can get the extents of its bounding box) in a table.
Place the next model, but before you do check to see if the new model’s bounding box (It’s size and Position) will collide with the first one using the stored information in the table.
Store that model’s Position and size in the table.
Select any Model in the workspace.
The outlined box that Roblox provides to show you the edges of the model are the bounding box.
GetBoundingBox and GetExtentsSize are shown here: Model | Roblox Creator Documentation
You can get the model’s bounding box then create a part with the returned CFrame, check for collision from that part, keep in mind it’ll be a square.rectangular part.
local model --ur model
local function createPart(size, position, orientation)
local part = Instance.new("Part")
part.Size = size
part.CFrame = orientation or part.CFrame
part.Position = position
part.Anchored = true
part.CanCollide = true
part.Parent = workspace
return part
end
-- method 1
local size = model:GetExtentsSize()
local part = createPart(size, model.PrimaryPart.Position)
-- method 2
local orientation, size = model:GetBoundingBox()
local part = createPart(size, model.PrimaryPart.Position, orientation)
(Just make a collision group, then change it settings and after that set every part of character or chests or something like that into this group) < this is for character spawn, elseif you wan’t models can’t collide with each other, do raycasting , then before placing chest make part in generated position , if the ray in direction find something that is too near , position is canceled and function runs again until the position is correct