Script erroring that a collision group already exists when it doesn't

Not sure if this is a bug or if I’m just doing something wrong, but in my module I am creating two different collision groups like this:


When running my code, I get this error
image
I’ve tried creating the collision groups in two different scripts but still have the same results.

2 Likes

Two options.

  1. Remove the CollisionGroups via command bar with RemoveCollisionGroup or before you create them. Good to organise your variables for this reason rather than calling functions after defining constants in your code, for the sake of readability.

Crude example:

local groupNameA = "A"
local groupNameB = "B"

pcall(PhysicsService.RemoveCollisionGroup, PhysicsService, groupNameA)
pcall(PhysicsService.RemoveCollisionGroup, PhysicsService, groupNameB)

PhysicsService:CreateCollisionGroup(groupNameA)
PhysicsService:CreateCollisionGroup(groupNameB)
  1. Handle a case for existing CollisionGroups in your code using a function. This function will check for existing groups: if the group already exists, don’t create it.

Crude example:

local playerCollisionGroupName = "PlayersForRagdollByTomatoes"
local HRPCollisionGroupName = "HRPForRagdoll"

-- Use a function to contain case verification
local function CreateCollisionGroup(collisionGroupName)
    local createdGroups = PhysicsService:GetCollisionGroups()
    local collisionGroupExists = {} do
        for _, createdGroup in pairs(createdGroups) do
            collisionGroupExists[createdGroup] = true
        end
    end

    if not collisionGroupExists[collisionGroupName] then
        PhysicsService:CreateCollisionGroup(collisionGroupName)
    end
end

Don’t know what else to say if not any of these. Check for conflicting code, use the collision group manager panel, anything.

4 Likes