I’m trying to disable collisions between the player character and any NPCs inside of a few folders. Right now the script looks like this, and it’s working as intended
PhysicsService:RegisterCollisionGroup("Bosses")
PhysicsService:RegisterCollisionGroup("Enemies")
PhysicsService:RegisterCollisionGroup("PlayerCharacters")
PhysicsService:CollisionGroupSetCollidable("Bosses", "Enemies", false)
PhysicsService:CollisionGroupSetCollidable("PlayerCharacters", "Bosses", false)
PhysicsService:CollisionGroupSetCollidable("PlayerCharacters", "Enemies", false)
local bossFolder = workspace:WaitForChild("Bosses")
local enemiesFolder = workspace:WaitForChild("Enemies")
local function watchCollisionGroups(parent, groupName)
for _,v in pairs(parent:GetDescendants()) do
if (v:IsA("BasePart")) then
v.CollisionGroup = groupName
end
end
parent.DescendantAdded:Connect(function(v)
if (v:IsA("BasePart")) then
v.CollisionGroup = groupName
end
end)
end
watchCollisionGroups(bossFolder, "Bosses")
watchCollisionGroups(enemiesFolder, "Enemies")
for _,player in pairs(Players:GetPlayers()) do
player.CharacterAdded:Connect(function(c)
watchCollisionGroups(c, "PlayerCharacters")
end)
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(c)
watchCollisionGroups(c, "PlayerCharacters")
end)
end)
I’ve checked the collision groups are actually being assigned:
Despite this the collision groups simply refuse to work and it has me stumped:
It’s probably something really simple I’m overlooking but if anybody has ideas I’d appreciate any help I can get
As a side note I’ve tried registering the collision groups through the menu in studio rather than at runtime but it doesn’t change anything. I’ve also verified the groups are actually registered prior to calling CollisionGroupSetCollidable.
The game is single player so there’ll never be more than one player character
(I threw in that line anyway as a hail mary just to see but unfortunately it doesn’t fix it)
Oh, sorry. I thought you were play-testing with two players, not an NPC.
A quick test got this to work for me. I did not modify your code at all; just threw an NPC into an Enemies folder in Workspace. Are you sure those mobs are under the correct folder?
Nope, this is a module script but it’s required server side. (The error message I’d get if I tried to call any of these methods on the client isn’t there either)
Yeah things are where I expect them to be and the CollisionGroup properties of everything inside of them get set correctly which makes this more confusing lol
i have found the problem
you are passing wrong objects into your watchCollisionGroups function
you are passing a Folder, and a Player object
while the function expects a Model, obviously
therefore, it simply doesn’t find any descendants of type BasePart in it
notice there are type declarations in Lua for that, and they can be strictified, too
your function signature may be changed like this
local function watchCollisionGroups(parent:Model, groupName:string):nil
It can’t be a problem with watchCollisionGroups or the arguments since the collision groups are all being set properly.
I can put together an rbxl file to see if there’s just something wrong on my end but it shouldn’t matter since everything is basically working properly except for the collision groups to.. do their thing after being set up.
returns Models inside the Bosses Folder; UPD: and the BaseParts inside those models
BUT it is NOT the way to do it
if v.CollisionGroup ~= 'Bosses'
is void - it is not checking anything, because your actual CollisionGroup property equals nil
you are experiencing the order of loading issues with your instances - this is why you believe your checks are working (and sometimes they don’t)
the way to counter this is to use a game:IsLoaded() method BOTH on the server and client
I can’t tell if you’re just baiting me here but the CollisionGroup properties of all BaseParts inside of all the models here are correct when I check them after starting a playtest.
This code worked for me. Turns out, instead of using parent.DescendantAdded, I changed it to parent.ChildAdded and just recursively updated collision groups for Models or Folders that were placed under the parent.
Also restructured the code a little cx, hope you don’t mind!
local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
PhysicsService:RegisterCollisionGroup("Bosses")
PhysicsService:RegisterCollisionGroup("Enemies")
PhysicsService:RegisterCollisionGroup("PlayerCharacters")
PhysicsService:CollisionGroupSetCollidable("Bosses", "Enemies", false)
PhysicsService:CollisionGroupSetCollidable("PlayerCharacters", "Bosses", false)
PhysicsService:CollisionGroupSetCollidable("PlayerCharacters", "Enemies", false)
local bossFolder = workspace:WaitForChild("Bosses")
local enemiesFolder = workspace:WaitForChild("Enemies")
local function setCollisionGroup(parent: Instance, groupName: string) : nil
for _, v in parent:GetDescendants() do
if not v:IsA("BasePart") then
continue
end
v.CollisionGroup = groupName
end
end
local function watchCollisionGroups(parent: Instance, groupName: string)
setCollisionGroup(parent, groupName)
parent.ChildAdded:Connect(function(v)
if v:IsA("Model") or v:IsA("Folder") then
setCollisionGroup(v, groupName)
end
end)
end
watchCollisionGroups(bossFolder, "Bosses")
watchCollisionGroups(enemiesFolder, "Enemies")
local function onPlayerAdded(player: Player)
local function onCharacterAdded(character: Model)
task.wait()
watchCollisionGroups(character, "PlayerCharacters")
end
if player.Character then
onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
end
for _, player in pairs(Players:GetPlayers()) do
task.spawn(onPlayerAdded, player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
This behaves the same as the original code but while setting up a demo file both this and the original work as intended so I can only assume I’ve managed to interfere with it somehow in the actual place without there being other parts in the way
Maybe it’s the physics character controllers or something I’ll leave an update for anyone with the same issue in the future if I do find what it is. I’ll mark yours as the solution too since it does work lol, thanks for the help :>