So I had this bug recently pop up in code that I haven’t touched for several weeks.
The code errors for no reason when I try to create a collision group.
This module function’s purpose is to remove collisions between player characters. Does anyone know why exactly my code breaks when I simply try to create a collision group? The error I am given tells me no useful information.
module.ToggleCollisions = function(bool)
local physics = game:GetService("PhysicsService")
if not bool then
physics:CreateCollisionGroup("ActivePlayerCollisons") --error here
physics:CollisionGroupSetCollidable("ActivePlayerCollisons", "ActivePlayerCollisons", false)
local activePlayers = workspace.GameStuff.ActivePlayers:GetChildren()
for i,v in pairs(activePlayers) do
local player = game.Players:FindFirstChild(v.Name)
if player then
local char = player.Character
if char then
--recurse char body parts and hats
module.PartsList = {}
local NewParts = module.RecurseParts(char)
for y, part in pairs(NewParts) do
physics:SetPartCollisionGroup(part, "ActivePlayerCollisons")
end
end
end
end
else
physics:RemoveCollisionGroup("ActivePlayerCollisons")
end
end
Unfortunately there aren’t any ways to check if a collision group exists by its name (Without erroring when it doesn’t exist) other than iterating over all collision groups with GetCollisionGroups
function collisionGroupExists(name)
for _,v in pairs(game:GetService("PhysicsService"):GetCollisionGroups()) do
if v.name == name then
return true
end
end
return false
end
You could always catch it with pcall but errors are prone to change
Hey Ripull, do you know if this code is executing from a LocalScript thread on the client or a server-side Script?
That’s the one thing that’s not clear here. I don’t believe it works on the client right now, which is something I have in my queue to fix (for setting at least, not creating).
Is it possible to change the error I received in the original post to “Could not create Collision Group, Collision group already exists” so that people encounter this mistake in future don’t get stuck on this like I did?
Yep, also an issue. @ContextLost desired behavior is:
Calling CreateCollisionGroup for an existing group will just return the existing group
Calling GetCollisionGroupId/Name when the collision group doesn’t exist returns nil instead of throwing an error (this is Lua – not a lower level language like C++)
Not sure if I’m experiencing the same error for a different reason, but anyone know if this has been fixed? I’ve got this:
Been having the error for a few months now, and since its not a very important part of a game I don’t develop much anymore I haven’t got round to fixing it.