Understanding collision groups

Heyo devforum! Recently, I have been trying to learn what I can do with collision groups because I have to use them inside of my game, but they are really confusing to me. There aren’t good tutorials on this on youtube, and the roblox wiki collision group article is really confusing. Is there any way someone can help me understand how to make collision groups through script?

4 Likes
local PhysicsService = game:GetService("PhysicsService")
 
local obstacles = "Obstacles"
local greenObjects = "GreenObjects"
 
-- Create two collision groups
PhysicsService:CreateCollisionGroup(obstacles) -- Creates a CollisionGroup with the name of 'Obstacles', all BaseParts in this Collision Group won't be able to touch the collision group below. (For now, it actually won't do anything, meaning they can collide eachother, but later on, it won't be able to.)
PhysicsService:CreateCollisionGroup(greenObjects)

After creating a collision group, you can use PhysicsService:SetPartCollisionGroup()to add a part to the collision group with the name you give. take this as a example:

local PhysicsService = game:GetService("PhysicsService")
 
local obstacles = "Obstacles"
local greenObjects = "GreenObjects"
 
-- Create two collision groups
PhysicsService:CreateCollisionGroup(obstacles)
PhysicsService:CreateCollisionGroup(greenObjects)
-- Add an object to each group
PhysicsService:SetPartCollisionGroup(workspace.Obstacle1, obstacles)
PhysicsService:SetPartCollisionGroup(workspace.GreenBall, greenObjects)

To control whether objects in a collision group collide with objects in a other collision group, call PhysicsService:CollisionGroupSetCollidable() , providing the two collision groups and a boolean true or false .

Get more info about all these stuff here: Collisions | Documentation - Roblox Creator Hub. Even if you did not understand, read it twice, or even multiple times, until you understand the concept, or even almost everything, if not, everything.

6 Likes

Wow! Thank you! I understand it a bit better now, so I decided to whip up a quick piece of code.

local physicsservice = game:GetService("PhysicsService")

local players = "Players"
local part = "Part"

physicsservice:CreateCollisionGroup(players)
physicsservice:CreateCollisionGroup(part)

game.Players.PlayerAdded:Connect(function(player)
	local otherplayers = game.Players:GetPlayers()
	table.remove(otherplayers,player.UserId)
	print("Success")
	for i, otherplayer in pairs(otherplayers) do
		otherplayer.CharacterAdded:Connect(function(character)
			for i, characterpart in pairs(character:GetChildren()) do
				if characterpart:IsA("BasePart") then
					physicsservice:SetPartCollisionGroup(characterpart, players)
					physicsservice:SetPartCollisionGroup(workspace.Part, part)
				end
			end
		end)
	end
end)

physicsservice:CollisionGroupSetCollidable(players, part, false)

The script is supposed to make it so that the player can touch the part, but other players can’t. What is the issue here?

2 Likes

What happened when you tried that? You got any errors? Also, what is specifically needed for the player not be able to touch the ‘part’?

Well I didn’t get any errors. Any errors I did get I fixed. I am trying to learn how to effectively learn collision groups because in a game I am making, I want it so that a player can interact with a part, but other player’s can’t interact with that person’s part, but their own. It is a very confusing process, so let me go into more depth. I am making a tower game filled with local objects. I want it so that when a player joins, their local object can not collide with any other players but that specific player. I want this same process to happen for the rest of the players. Right now, I just made a part inside the workspace and wrote this so that when two people join, the person who joins makes it so that the other player’s cant collide with the part. Wait, can collision groups be local?

If your adding the objects locally then only the local player can see/interact with the object so collision groups aren’t needed.

1 Like

Right, but it is a different case with objects that have constraints. I created objects locally with constraints, but everyone can still see and interact with the same thing.