Collision groups not working

im trying to make a wall that survivors can go through but killers can’t, but for some reason the collision groups don’t work for it. this is in server script.

local PhysicsService = game:GetService("PhysicsService")

local walls = "Walls"

local survivors = "Surv"
local killers = "Kill"

PhysicsService:RegisterCollisionGroup(walls)
PhysicsService:RegisterCollisionGroup(survivors)
PhysicsService:RegisterCollisionGroup(killers)

script.Parent.Wall.CollisionGroup = walls

local players = game:GetService("Players"):GetPlayers()

spawn(function()
	while true do
		wait()
		for _, player in pairs(players) do
			if player.Character.survivor.Value == true then
				for i, characterpart in pairs(player.Character:GetChildren()) do
					if characterpart:IsA("BasePart") then
						characterpart.CollisionGroup = survivors
					end
				end
			end
			if player.Character.killer.Value == true then
				for i, characterpart in pairs(player.Character:GetChildren()) do
					if characterpart:IsA("BasePart") then
						characterpart.CollisionGroup = killers
					end
				end
			end
		end
	end
end)

PhysicsService:CollisionGroupSetCollidable(survivors, walls, false)
PhysicsService:CollisionGroupSetCollidable(killers, walls, true)

You’re using GetPlayers presumably before any player actually loads in so the script will be looping nothing.

You should connect a function to Players.PlayerAdded event, and then connect a function to the Player.CharacterAdded event. Then create connections to get and detect new parts inside the character. EG:

local Players = game:GetService('Players')

local survivors = "Surv"
local killers = "Kill"

local function UpdateCollisionGroups(
	Character:Model 
)
	local CollisionGroup = Character.killer.Value and killers 
		or survivors
	
	for _,Part:BasePart in (Character:GetChildren()) do 
		if not Part:IsA("BasePart") then
			continue
		end
		
		Part.CollisionGroup = CollisionGroup
	end
end
 
local function NewPlayer(player:Player)  
	local function NewCharacter(Character:Model)  
		local function NewPart(Part:BasePart)
			if not Part:IsA("BasePart") then
				return
			end
			
			UpdateCollisionGroups(Character)	-- Fire Update Function when a new part is added
		end
		
		Character.ChildAdded:Connect(NewPart)
		for _,v in pairs(Character:GetChildren()) do 
			NewPart(v)
		end
	end
	
	NewCharacter(player.Character or player.CharacterAdded:Wait())
	player.CharacterAdded:Connect(NewCharacter) -- Will fire NewCharacter function upon new character created 
end

Players.PlayerAdded:Connect(NewPlayer)	-- Will fire NewPlayer function when a player joins

You can also then connect a :GetPropertyChangedSignal event to call the UpdateCollisionGroups function when the BoolValue “killer” is changed. Eg

Character.killer:GetPropertyChangedSignal("Value"):Connect(function()
UpdateCollisionGroups(Character)
end)

(Though realistically killer variable should be parented to the Player instance, not the character)

The problem isn’t how you’re setting collision groups, it’s with how you’re grabbing the players. local players = game:GetService("Players"):GetPlayers() is only getting called once in your script, outside of the loop. Really, it should be getting called inside of the loop so that any new players that join will be acknowledged.

When the script runs, the players table will be empty because no players have had time to join, and any new players that join later on also won’t get added.

Edit: you’re also not waiting for the characters to load, so you’ll be getting nil errors.

thank you this works. thank you for the help