I need help creating assigned spawn locations for teams

Basically, I was able to make a script that allows players when they join, to be assigned a spawn location which is only accessed by them until they end up leaving, then the next player who joins will end up taking that spawn location. I used a bool value to determine this which sets to false when a spawn location is taken. I manage to get this to work with all players who aren’t assigned to teams but however, I was unable to get it working with players on different teams. I’m making a prison game and I want a team of inmates who will all have assigned spawns within the cells placed in my prison based on who joins and as for the police and criminals, It doesn’t matter if they have assigned spawns or not.

local function onPlayerJoin(player)

	for _, spwn in pairs(game.Workspace.Spawns:GetChildren()) do

		if spwn.Free.Value then

			player.RespawnLocation = spwn

			spwn.Free.Value = false



			local spawnTag = Instance.new('StringValue')

			spawnTag.Name = 'SpawnTag'

			spawnTag.Value = spwn.Name

			spawnTag.Parent = player

			break

		end

	end

end



local function onPlayerExit(player)

	local spawnTag = player.SpawnTag.Value

	print(spawnTag)

	local spwn = game.Workspace.Spawns[spawnTag]

	spwn.Free.Value = true

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

It is a server script.