Collision Group Problem

Why isn’t this running

There’s a region that a max number of players can enter. If it hits max then characters outside the regions collision group changes so they cant go in. When someone leaves and a player can now enter then everyone’s collision group is reset


local PhysicsService = game:GetService("PhysicsService")

PhysicsService:CreateCollisionGroup("PlayersOut")
PhysicsService:CreateCollisionGroup("PlayersAllowed")
PhysicsService:CreateCollisionGroup("Region")

PhysicsService:SetPartCollisionGroup(game.Workspace.Region, "Region")
PhysicsService:CollisionGroupSetCollidable("PlayersOut","Region", true)
PhysicsService:CollisionGroupSetCollidable("PlayersAllowed","Region", false)

function KickOut(CharactersOut)
	for i = 1,#CharactersOut do
		local CharacterParts = CharactersOut[i]:GetChildren()	
		for j = 1,#CharacterParts do		
			if CharacterParts[j]:IsA("BasePart") then		
				PhysicsService:SetPartCollisionGroup(CharacterParts[j], "PlayersOut")
			end		
		end		
	end
	PhysicsService:CollisionGroupSetCollidable("PlayersOut","Region", true)
end

function LetBackIn(Characters)
	for i = 1,#Characters do
		local CharacterParts = Characters[i]:GetChildren()
		for j = 1,#CharacterParts do
			if CharacterParts[j]:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(CharacterParts[j], "PlayersAllowed")
			end
		end
	end
	PhysicsService:CollisionGroupSetCollidable("PlayersAllowed","Region", false)
end

function CharacterStats()
end

game.Workspace.Region.PlayerInside.Changed:Connect(function()
	--game.Workspace.Region.BillboardGui.TextBox.Text = tostring(game.Workspace.Region.PlayerInside.Value.. "/".. DesiredAmmountOfPlayersDancing)
	if game.Workspace.Region.PlayerInside.Value >= DesiredAmmountOfPlayersDancing then
		local CharInsideArea, CharOutsideAre = CharacterStats()
		KickOut(CharOutsideAre)
	else
		local CharInsideArea, CharOutsideAre, AllCharacters = CharacterStats()
		LetBackIn(AllCharacters)
	end
end)

I have no clue :confused:

FYI. I have a function so if a new character spawns its collision group is automatically set to the relevant collision Group

(Basically same as Kickout and Letbackin functions but for a single character)

Incase anyone thinks its because it doesn’t acquaint for characters that spawn after the function fires

Bump

(I need to use more Characters)