2 collision groups collide and don't collide with each other randomly

Hey there!

Basically I’m having a function that returns pet from Replicated Storage and adds all of it’s descendant parts to collision group which should not collide with players.

function returnPet(pet)
	if petFolder:FindFirstChild(pet) then
		local newPet = petFolder:FindFirstChild(pet):Clone()
		for i,v in pairs(newPet:GetDescendants()) do
			if v:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(v,"Pets")
			end
		end
		
		return newPet
	end
end

The issue I’m currently experiencing is that it literally works and doesnt’ work randomly.
Once I test it it works and then it doesnt. If I add print statement to check if anything is wrong it starts working and then stops working again.

Also this is my first post on dev forum, so corect me if I haven’t done something correctly.

Thank you!

2 Likes

Could you show a list of existing collision groups and their collision relationships? You can do this via the Collision Groups view in the Advanced section of the Model tab. This code isn’t enough to go off of to determine what a probable cause may be for this occurring.

Are you also able to pinpoint where exactly you are experiencing unpredictable behaviour? Can it be reduced to a part of your code where the behaviour starts occurring (such as setting collision groups, changing collision group relations, so on)? What of your prints, could more information be provided for that? Did they all work or not? Where did you put them? What was in them?

A whole lot more information about your circumstances is necessary to understand the scenario you’re encountering, let alone to try and help you work towards a solution.

1 Like

Hey,

It pretty much feels unpredictable and I wasn’t able to pinpoint where exactly it starts.
One thing is that if I adedd a print statement into the loop to print the names of BaseParts inside character it literally just started working.

As in for the list, I’m pretty sure it’s set-up properly.

I’ll provide code for adding player into the collision group :

function added(character)
	local newTag  = game.ServerStorage:WaitForChild("Tag"):Clone()

	newTag.Parent = character:WaitForChild("Head")
	wait(.2)
	gameEvent:FireClient(game.Players:GetPlayerFromCharacter(character),"Destroy Tag")
	
	character:WaitForChild("Humanoid").HealthDisplayDistance = 0
	character:WaitForChild("Humanoid").HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff
	character:WaitForChild("Humanoid").NameDisplayDistance = 0
	character:WaitForChild("Humanoid").NameOcclusion = Enum.NameOcclusion.NoOcclusion
	
	for i,v in pairs(character:GetDescendants()) do
		if v:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(v,"Players")
		end
	end
	

end

It’s inside module script that connects whenever a character is added.

Sorry for not providing these information earlier.

Thanks for providing this information, it helps out. To further pin down this unpredictability, are you currently able to reproduce the behaviour when it starts working and not so much? If you are able to do so, while the issue is occurring, can you confirm that the CollisionGroupId of all character and pet parts are non-zero? If any are zero, that means your code doesn’t account fully for all parts.

Hey,

it’s literally not reproducable and how would I check if they are non-zero?

Just figured out that it seems to work only when in-game (not in studio)
.

In the properties menu, you will see a “CollisionGroupId” entry if it’s a BasePart instance. All you’d need to do is check that the value of that isn’t 0. You can also create a for loop to go through all parts that should have a collision group and, if it’s 0, print it out.

local flagged = 0
for _, part in ipairs(model:GetDescendants()) do
    if part:IsA("BasePart") and part.CollisionGroupId == 0 then
        flagged = flagged + 1
    end
end
if flagged > 0 then
    print(flagged, "parts were flagged using the incorrect collision group")
end

For future-proofing, to ensure that all the parts you’re working with properly get added in: besides the initial setting of the BasePart, make sure also to check for new BasePart descendants in the character and set their collision groups. Characters aren’t guaranteed to be fully constructed after CharacterAdded fires (not until Roblox finally addresses Avatar Loading Ordering Events Improvements).

Hmm, just checked it out and it seems like Character parts aren’t added into the collision group, checking for new BasePart’s should fix it, right?

Seens to work great now, thank you!