Collision group command bar export and import

For some reason there are no methods to import and export collision group between place files though It’s understandable since it doesn’t happen too often since you most likely will stay working with them in one place.

However, somehow this has happened to me twice cuz I thought it was a good idea to refactor my game so I decided to somewhat automate it and I want to share it. I didn’t put it in #resources:community-resources because I don’t think it’s too significant.

First is the script which jsonifies the collision group data:

JSONIFY collision group data
local PhysicsService = game:GetService("PhysicsService")
local HttpService = game:GetService("HttpService")

local data = PhysicsService:GetCollisionGroups()
local collisionGroupNames = {}
for _, groupData in pairs(data) do
	table.insert(collisionGroupNames, groupData.name)
end

local collisionGroupCollide = {}
for _, collisionGroup in pairs(collisionGroupNames) do
    if collisionGroup == "Default" then
        continue --skip it cannot modify default collisionGroup
    end

	local thisGroupCannotCollideWith = {}
	collisionGroupCollide[collisionGroup] = thisGroupCannotCollideWith
	for _, otherGroup in pairs(collisionGroupNames) do
		local cannotCollide = not PhysicsService:CollisionGroupsAreCollidable(collisionGroup, otherGroup)
		if cannotCollide then
			table.insert(thisGroupCannotCollideWith, otherGroup)
		end
	end
end

local encoded = HttpService:JSONEncode(collisionGroupCollide)
print(encoded)

Using the Json data we got as a string and pasting it into the command bar:

Reconstruct collision group from JsonData
--insert your json data as a string in this variable
local collisionGroupJSONData = '{"Projectiles":["NonHittable","Debris"],"Debris":["NonHittable","Debris","Vehicle","Projectiles"],"Vehicle":["NonHittable","Debris"],"NonHittable":["Default","NonHittable","Debris","Vehicle","Projectiles"],"Plugin_StudioTweaks_BoundingBox":["Default"]}'

local function removeAllCollisionGroups()
    local collisionGroupData = game.PhysicsService:GetCollisionGroups()
    for _, groupData in pairs(collisionGroupData) do
        if groupData.name == "Default" then
            continue --ignore the default
        end
        game.PhysicsService:RemoveCollisionGroup(groupData.name)
    end
end
removeAllCollisionGroups()--Prevent overlapping collision group error

local decoded = game.HttpService:JSONDecode(collisionGroupJSONData)

for collisiongroup, _ in pairs(decoded) do
    game.PhysicsService:CreateCollisionGroup(collisiongroup)
end

for collisiongroup, groupsThatItCannotCollideWith in pairs(decoded) do
    for _,groupThatItCannotCollideWith in pairs(groupsThatItCannotCollideWith) do
        game.PhysicsService:CollisionGroupSetCollidable(collisiongroup,groupThatItCannotCollideWith,false)
    end
end

Before:

After:

Hope this saves the pain of typing each collision group and ticking those boxes. (Really minor but gotta automate everything like in factorio)

24 Likes

This will help alot of people, good job!

3 Likes

Great resource! This helped me a lot.

2 Likes