Need help with Player Collision Filter

Hello guys, I have this short piece of code I need help with, My goal is to simply let the player in a barrier, I’ve tried different scripts and 3 devhub articles to try and help me but each would do the same result. It be nice if you guys can help me thanks.I spent a whole day on this script

local PhysicsService = game:GetService("PhysicsService")

local obstacles = "Obstacle"
local greenObjects = "Green"
local playerCollisionGroupName = "Players"

PhysicsService:CreateCollisionGroup(obstacles)
PhysicsService:CreateCollisionGroup(greenObjects)
PhysicsService:CreateCollisionGroup(playerCollisionGroupName)
PhysicsService:SetPartCollisionGroup(workspace.barrier, obstacles)
PhysicsService:SetPartCollisionGroup(playerCollisionGroupName, obstacles)

PhysicsService:CollisionGroupSetCollidable(obstacles, playerCollisionGroupName, false)
PhysicsService:CollisionGroupSetCollidable(greenObjects, obstacles, false)

local previousCollisionGroups = {}
---supposed problem below
local function setCollisionGroup(object)
	if object:IsA("BasePart") then
		previousCollisionGroups[object] = object.CollisionGroupId
		PhysicsService:SetPartCollisionGroup(object, playerCollisionGroupName)
	end
end

local function setCollisionGroupRecursive(object)
	setCollisionGroup(object)
	
	for _, child in ipairs(object:GetChildren()) do
		setCollisionGroupRecursive(child)
	end
end

game.Players.PlayerAdded:Connect(setCollisionGroupRecursive)

Sorry if this has a simple solution but I just started this today.

You’re trying to set the collision groups of the player, not the character. Try this instead:

CODE VERSION 1
local PhysicsService = game:GetService("PhysicsService")

local obstacles = "Obstacle"
local greenObjects = "Green"
local playerCollisionGroupName = "Players"

PhysicsService:CreateCollisionGroup(obstacles)
PhysicsService:CreateCollisionGroup(greenObjects)
PhysicsService:CreateCollisionGroup(playerCollisionGroupName)

PhysicsService:SetPartCollisionGroup(workspace.barrier, obstacles)
PhysicsService:SetPartCollisionGroup(playerCollisionGroupName, obstacles)

PhysicsService:CollisionGroupSetCollidable(obstacles, playerCollisionGroupName, false)
PhysicsService:CollisionGroupSetCollidable(greenObjects, obstacles, false)

local previousCollisionGroups = {}

--go through every descendant of the character and make any part part of the player collision group
function setCollisionGroup(character)	
	for _, desc in pairs(character:GetDescendants()) do
		if desc:IsA("BasePart")then
			previousCollisionGroups[desc] = desc.CollisionGroupId
			PhysicsService:SetPartCollisionGroup(desc, playerCollisionGroupName)
		end
	end
end

--set charater's collisions when they load into the map:
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		setCollisionGroup(character)
	end)
end)

EDIT: it was erroring because
playerCollisionGroupName is not a part in
PhysicsService:SetPartCollisionGroup(playerCollisionGroupName, obstacles)
and it’s expecting a part.

made it instead:
PhysicsService:CollisionGroupSetCollidable(playerCollisionGroupName, obstacles, true)
and seems to work fine.

change this to true/false depending on if you want the players to collide with obstacles

CODE VERSION 2:
local PhysicsService = game:GetService("PhysicsService")

local obstacles = "Obstacles"
local greenObjects = "GreenObjects"
local player = "Player"

PhysicsService:CreateCollisionGroup(obstacles)
PhysicsService:CreateCollisionGroup(greenObjects)
PhysicsService:CreateCollisionGroup(player)

PhysicsService:SetPartCollisionGroup(workspace.barrier, obstacles)
PhysicsService:CollisionGroupSetCollidable(greenObjects, obstacles, false)
PhysicsService:CollisionGroupSetCollidable(player, obstacles, false)

local previousCollisionGroups = {}

function setCollisionGroup(model)
	for _, desc in pairs(model:GetDescendants()) do
		if desc:IsA("BasePart")then
			previousCollisionGroups[desc] = desc.CollisionGroupId
			PhysicsService:SetPartCollisionGroup(desc, player)
		end
	end
end

for _,desc in pairs(workspace:GetDescendants())do
	if desc.Name== "Boat" then
		local Boat = desc
		setCollisionGroup(Boat, greenObjects)	
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		setCollisionGroup(character, player)
	end)
end)
2 Likes

Perform the recursive on the character not the player.


local function CharacterAdded(character) 
    setCollisionGroup(character)
end


local function PlayerAdded(player) 
    CharacterAdded(player.Character or player.CharacterAdded:Wait())
    player.CharacterAdded:Connect(CharacterAdded)
end

for _, plr in pairs(Services.Players:GetPlayers()) do
    spawn(function() 
        PlayerAdded(plr)
    end)
end

Services.Players.PlayerAdded:Connect(PlayerAdded)