I have a module that spawns objects around my map. I have spawn-points that are place in certain places and the objects I want to spawn will spawn at a random spawn-point. I’m not sure how to relocate the objects that spawn inside each other.
local itemSpawnerModule = {}
local Replicated = game:GetService("ReplicatedStorage")
local spawnPoints = workspace.itemSpawnPoints:GetDescendants()
local puzzlePieces = workspace.puzzlePieces.Part
local newPuzzleFolder = workspace.piecesSpawn
local puzzlesCollected = Replicated.itemValues.puzzlesCollected
local amount = 25
local puzzleTable = {}
function itemSpawnerModule.spawnCollide()
for i, v in pairs(spawnPoints) do
if v:IsA("Part") then
v.CanCollide = false
end
end
end
function itemSpawnerModule.collectPuzzles()
Replicated.remoteEvents.objectsCollected:FireAllClients(puzzlesCollected, puzzleTable)
end
function itemSpawnerModule.proximtyActivated(puzzle)
puzzle.ProximityPrompt.Triggered:Connect(function()
puzzle.Parent = Replicated.collectedItems
puzzlesCollected.Value += 1
itemSpawnerModule.collectPuzzles()
end)
end
function itemSpawnerModule.morePuzzles()
for i, v in pairs(spawnPoints) do
local spawnsOpens = spawnPoints[math.random(1, #spawnPoints)]
local newPiece = puzzlePieces:Clone()
newPiece.Parent = newPuzzleFolder
newPiece.Anchored = true
table.insert(puzzleTable, newPiece)
if spawnsOpens:IsA("Part") then
newPiece.CFrame = spawnsOpens.CFrame
if #puzzleTable == amount then
break
end
itemSpawnerModule.proximtyActivated(newPiece)
end
for i, v in pairs(newPiece:GetTouchingParts()) do
if v.Name == "Part" and spawnsOpens:IsA("Part") then
v.CFrame = spawnsOpens.CFrame
end
end
end
end
return itemSpawnerModule
What do guy mean with relocate? There’s different ways of doing this, the easiest would be to make them unanchored which will make them go out of where the part and then anche le them if you need
When the game starts every object goes to a random spawn point but some objects spawn inside each other at the same spawn point. I want to be able to detect when that happens and then move the object to a empty spawn point.
I can’t use the touch event on 2 parts that are anchored. I’m just trying to figure out how I can move them somewhere else or if I should be doing something totally different.
You could remove an element from the spawnPoints table once its been chosen, so it won’t be chosen again. But in case you want to keep the spawnPoints table unchanged, I modified your function a bit to use a temporary table to keep spawnPoints unchanged but still use my approach.
function itemSpawnerModule.morePuzzles()
local temp = spawnPoints
for i, v in pairs(spawnPoints) do
local index = math.random(1, #temp)
local spawnsOpens = temp[index]
table.remove(temp, index)
local newPiece = puzzlePieces:Clone()
newPiece.Parent = newPuzzleFolder
newPiece.Anchored = true
table.insert(puzzleTable, newPiece)
if spawnsOpens:IsA("Part") then
newPiece.CFrame = spawnsOpens.CFrame
if #puzzleTable == amount then
break
end
itemSpawnerModule.proximtyActivated(newPiece)
end
for i, v in pairs(newPiece:GetTouchingParts()) do
if v.Name == "Part" and spawnsOpens:IsA("Part") then
v.CFrame = spawnsOpens.CFrame
end
end
end
end