You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? A spawning system
-
What is the issue? It’s functional but triggers a function loop for some reason
-
What solutions have you tried so far? looking up similar problems, to no result
local function findspawn()
if spawns[1] ~= nil then --SHOULD check if there's any avaliable spawns
local selected = math.random(1, #spawns) --selects one of the avaliable spawns
if spawns[selected]:GetChildren()[1] == nil then -- checks if it's free
local me = spawns[selected]
EventCosmetic()
local sabo = game.ReplicatedStorage.Sabotuer:Clone()
sabo.Parent = spawns[selected]
sabo:MoveTo(spawns[selected].Position)
sabo:PivotTo(spawns[selected].CFrame)
spawns[selected].ChildRemoved:Connect(function()
table.insert(spawns, me) -- adds itself back to the spawns list
for _, i in pairs(spawns) do
local num = #rawspawns -- check if all the spawns are empty by comparing the "spawns" table and the total amount of spawns and seeing if theyre the same
if num == #spawns then
if game.SoundService.SabotageTogether.Playing == true then
game.SoundService.SabotageTogether.Playing = false --stop event and music if all are dead
event.Value = "None"
end
end
end
end)
table.remove(spawns, selected)
else
findspawn()
end
else
print("NOMORELEFT!")
end
end
The sabotuer npcs get destroyed when interacted with and EventCosmetic just plays a sound, the findspawn() function gets played as many times as there are spawns avaliable in one of the events and just once in the other.