[Error] Both collision groups must be registered

In my minigolf game, i’m trying to make your ball the only one you can hit so griefing other players isn’t possible in the game.

Currently, I have it set up so that when a player joins the game it makes a collision group for your ball and your putter in a collision group. This all works fine but it is at line 16 that the error is from where I try to set the putter to not be able to collide with other player’s balls.

local PhysService = game:GetService("PhysicsService")

game.Players.PlayerAdded:Connect(function(player)
	
	local ball = PhysService:RegisterCollisionGroup(player.Name.." Ball")
	PhysService:CollisionGroupSetCollidable(player.Name.." Ball" ,"Player",false)

	local ball = PhysService:RegisterCollisionGroup(player.Name.." Putter")
	PhysService:CollisionGroupSetCollidable(player.Name.." Putter" ,"Player",false)
	PhysService:CollisionGroupSetCollidable(player.Name.." Putter" ,"Default",false)
	PhysService:CollisionGroupSetCollidable(player.Name.." Putter" ,"Wall",false)
	
	for _, groups in pairs(PhysService:GetRegisteredCollisionGroups()) do
		if groups["Name"] ~= player.Name.." Ball" or player.Name.." Putter" or "Default" or "Wall" then
			PhysService:CollisionGroupSetCollidable(player.Name.." Putter" , groups ,false)
		end
	end
end)

If anyone could help I would greatly appreciate it and if you know a better way I could do this feel free to tell me!

2 Likes

I think the error might be stemming from the reference to “groups” here:

Because groups is referring to the table returned from PhysicsService:GetRegisteredCollisionGroups(), but PhysicsService:CollisionGroupSetCollidable() only accepts strings for the first two arguments, you’d need to update that to groups["Name"] for that method to accept it.


On a side note, for the prior conditional statement, are you wanting to make sure that groups["Name"] is not equal to any of the names listed afterwards?

If so, that may need to be revised, because currently, that is only checking if this first condition is equal to false:

groups["Name"] ~= player.Name.." Ball"

Once you get to the or operator, it begins evaluating that next condition on its own, meaning that it’s just checking if player.Name.." Putter, "Default" or "Wall" return a truthy value. As a result, that conditional statement passes and runs the next line of code no matter what. You can test this by running an empty script that just checks for a string and it’ll run the code within the condition:

-- Example
if "Default" then
    print("Hi!")
end

Essentially, it would need to be updated to something like this to make sure that the name of the Collision Group doesn’t match any of the ones you mentioned there:

-- Example revision


-- Code that created new Collision Groups for the player would be before this revised section

local namesToCheckFor = {
	[1] = player.Name.." Ball",
	[2] = player.Name.." Putter",
	[3] = "Default",
	[4] = "Wall"
}

for _, groups in PhysService:GetRegisteredCollisionGroups() do
    if not table.find(namesToCheckFor, groups["Name"]) then
	    PhysService:CollisionGroupSetCollidable(player.Name.." Putter", groups["Name"], false)
    end
end

Or maybe it would let you reference the variables that you used to create the collision groups in the first place so you could replace what was manually input for index 1 and 2 with “ball” (and to make sure that the variable names weren’t overlapping, the variable for the second collision group created could be renamed to “putter” to more accurately represent the name of the collision group it was creating).


There are probably better ways of approaching this, but it’s what I thought of right now as a basic example. Hope that this was useful :smile:

1 Like

And also, one last thing that I thought I should mention since it could be completely game-changing for you if this wasn’t considered already.


Based on the code included here, it appears as if there are at least 3 Collision Groups in the game before anyone joins:

  • Player
  • Default
  • Wall

Then, there are 2 new Collision Groups created for each player:

  • player.Name.." Ball"
  • player.Name.." Putter"

Unfortunately, because there is currently a maximum limit of 32 Collision Groups per place (as of March 2024), this would mean that your game would only be able to have 14 players in a server at a time before you reached the maximum amount of Collision Groups

  • (because 14 players * 2 new collision groups = 28. Then, add the 3 Collision Groups that were there already and that amounts to 31 Collision Groups).

Considering other mini golf games such as Super Golf have a maximum server size of 20 players, they are probably finding a way to consolidate some of those collision groups into singular ones so that they do not reach the limit as quickly.

1 Like

Thank you for the response. I am no longer using those collision groups now that you have told me that.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.