PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

CollisionGroupId

BasePart

int

The CollisionGroupId property describes the ID number of the part’s collision group. Parts start off in the Default group whose ID is 0. Although this property can be directly changed, it is recommended to instead manipulate the collision group of a part using the name of the group with the /PhysicsService/SetPartCollisionGroup function. You can find the ID of a collision group by using /PhysicsService/GetCollisionGroupId.

This value cannot be negative, and cannot exceed /PhysicsService/GetMaxCollisionGroups. Invalid IDs are clamped.


Code Samples


PhysicsService:GetCollisionGroupId1

local physicsService = game:GetService("PhysicsService")

-- returns a valid group id (int) or nil
function getGroupId(name)
	-- GetCollisionGroupId will throw if it does not exist
	local ok, groupId = pcall(physicsService.GetCollisionGroupId, physicsService, name)
	return ok and groupId or nil
end

-- returns a valid group id (int) or nil
function getOrCreateGroupId(name)
	local ok, groupId = pcall(physicsService.GetCollisionGroupId, physicsService, name)
	if not ok then
		-- Create may fail if we have hit the maximum of 32 different groups
		ok, groupId = pcall(physicsService.CreateCollisionGroup, physicsService, name)
	end
	return ok and groupId or nil
end
	
print(getGroupId("Test")) -- prints nil
print(getOrCreateGroupId("Test")) -- prints 1
print(getGroupId("Test")) -- prints 1