Part not adding to collision group through script

  1. I want a part on top of the player’s head, not to be collidable with any game objects except enemies.

  2. **I have set collision groups for the single hitbox part and a different group for the enemies. Everything works when I manually add the hitbox part to its collision group, but I cannot add the part to the collision group through script.

  3. **I have added a repeat until line to repeat the part adding to the collision group until the game finds the part in the collision group to make sure the part gets added. I added a print line after the repeat line that gets fired meaning the repeat line finishes. This script is in ServerScriptService and it is a ServerScript.

CollisionGroups

local physicsservice = game:GetService("PhysicsService")
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	repeat
		physicsservice:SetPartCollisionGroup(player.Character:WaitForChild("HeadCollidePart"),"PlayerHeadHitbox")
	until physicsservice:CollisionGroupContainsPart("PlayerHeadHitbox",player.Character:WaitForChild("HeadCollidePart"))
	end)

1 Like

based on the new line in the SS, it is possible that it should be “PlayerHead Hitbox”. May not be but ah well, worth a check

1 Like

I checked. It is all one word. “PlayerHeadHitbox”

1 Like

Upon more checking, I find that the CollisionGroupId is correct as well. I am not sure why this is not working, as it should even work without the script since the part is in the correct CollisionGroup by default.

1 Like

Upon even more checking, It seems that when I join the game, the part’s collision group gets changed from group 6 to group 7. After changing it back to 6, it works as intended. I am wondering why the ColliisionGroupId is changing automatically. I went through all scripts using the Find All / Replace All tool and verified only two mentions of Physics Service in scripts, meaning that no scripts made by me are altering collision groups to my knowledge.

1 Like

I want to reiterate, there has yet to be a solution to this thread.

1 Like

Have you found a solution? I’m experiencing this issue as well.

I’ve tried setting a part’s collision group in a script using PhysicsService:SetPartCollisionGroup() but the part doesn’t get added to the collision group and still collides with undesired objects.

I’ve also tried setting the part’s collision group directly using the CollisionGroupId property, but that doesn’t work either.

When I manually set the collision group of the model, things work fine, but that is not a feasible option in my case.

Hopefully this gets solved soon.

Please let me know if you find a solution.

1 Like

So I figured out how to get setting a part’s collision group via script to work. Instead of setting it’s collision group multiple times in the script, I just set the specified part’s collision group separately from other objects, rather than setting all parts’ collision groups at once and then changing the collision groups of the desired parts.

I’m not sure if that will be of much use to you, but that’s how I got it to work. Hope it helps!

1 Like

I should have closed this topic due to me finding the solution. My solution was waiting a little longer in the script before setting the collision groups. I ended up having to wait an extra two seconds after player.ChatacterAdded:Wait() for it to work. I am glad you found a solution to your script though. Thanks for adding to the topic!

1 Like