Get SpawnLocations from Team

I have noticed there is no built-in function for getting SpawnLocations from a team.
I can do this:

local function getSpawnsFromTeam(team)
	local t = {}
	for _, v in pairs(workspace:GetDescendants()) do
		if v:IsA("SpawnLocation") and v.TeamColor == team.TeamColor then
			table.insert(t, v)
		end
	end
	return t
end

But looping through all instances in the workspace seems kind of unnecessary.

Am I overlooking something? Is there maybe a better way to do this?

If anyone wants to take this to a feature request I would be glad! I sadly can’t post there.

1 Like

You can try storing all the spawnlocations to a folder and use the :GetChildren() function instead

2 Likes

Could you not just put the spawns into 1 folder/model in workspace?

1 Like

hahaha had the same idea as you

Store all SpawnLocations in a table so you don’t have to visit every instance every time, or tag them and get a table of SpawnLocations using CollectionService

2 Likes

Thanks for your suggestions @ThanksRoBama @Wynsage and @PolyyDev.

Unfortunately due to the dynamic nature of the spawns (getting cloned in and out of the workspace) it is not possible to keep a static reference to them.

I will try using CollectionService.

A built-in function would still be nice tho…

Can’t you just add/remove them from a table when they’re created or destroyed?

1 Like

Not really, since another script handles creating or destroying them.

But I think it’s fine for my use case, since I don’t have to get the spawns to often.

I find it weird however that no function (by Roblox) exists for this…

1 Like

Are both of the scripts on the server? If so you could use the shared table to access it across different scripts

1 Like