Issue with checking for Collision Groups

Hi, I have a code for this tool, and the tool is given to the player at the start of every round, and removed at the end of every round:

local grenade = script.Parent
player = game.Players.LocalPlayer


local re = grenade:WaitForChild("ThrowRE")
local ps = game:GetService("PhysicsService")


local function CreateCollisionGroup(collisionGroupName)
	local createdGroups = ps:GetCollisionGroups()
	local collisionGroupExists = {} do
		for _, createdGroup in pairs(createdGroups) do
			collisionGroupExists[createdGroup] = true
		end
	end

	if not collisionGroupExists[collisionGroupName] then
		ps:CreateCollisionGroup(collisionGroupName)
	end
end
CreateCollisionGroup("Grenade")
CreateCollisionGroup("Character")

ps:CollisionGroupSetCollidable("Character", "Grenade", false)



re.OnServerEvent:Connect(function(plr, mouseHit)

	local grenadeCopy = grenade.Handle:Clone()


	for i, descendant in pairs(plr.Character:GetDescendants()) do

		if descendant:IsA("BasePart") then ps:SetPartCollisionGroup(descendant, "Character") end
	end

	ps:SetPartCollisionGroup(grenadeCopy, "Grenade")


	local velocity = (Vector3.new(0, -game.Workspace.Gravity * 0.1, 0))

	grenadeCopy.Velocity = velocity
	grenadeCopy.CanCollide = true
	grenadeCopy.Parent = workspace


	
	grenade:Destroy()
	wait(1.5)

	grenadeCopy.Smoke.Enabled = true
	grenadeCopy.Explode:Play()
	
	wait(grenadeCopy.Explode.TimeLength)
	grenadeCopy:Destroy()

end)

Since the tool gets given again every round, there seems to be a need to delete the collision groups (Or alternatively check they are there so they dont have to be remade). The issue is, I get this error when the player who gets the tool loads in the round for the SECOND time (he has already been given and then had removed the tool once before)

Could not create collision group, one with that name already exists. 

Any ideas why it is still trying to create a collision group when the function should prevent that?
Thanks

GetCollisionGroups returns an array of dictionaries containing information on each group. Your code assumes that it returns an array of group names.

Try changing this:

To this:

collisionGroupExists[createdGroup.name] = true
1 Like

Hi sorry for the late reply.
I tried that, and I get this error when first loading into the round

Players.Player2.Backpack.SmokeBomb.SmokeGrenadeServer:13: table index is nil

can you send the new code? make sure name is all lowercase

Oh cant believe I missed that I had it as .Name (Sorry been doing a lot with the likes of .Value so used to capilisation haha)