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

GetCollisionGroupId

Returns the id of the collision group with the specified name.

This function will throw an error if no group with the given name exists.

Example

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


Parameters

Name Type Default Description

Returns

Return Type Summary

int


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