I’m having problems with the Physics service I just learned. This script prevents npc from touching players. But this script works in my test arena (that is, only baseplate in it). In my main game, npc can touch players.
Most likely cause is that all the characters’ parts are not having their collision groups set properly. The current avatar loading events ordering does not guarantee that parts will exist in the character when CharacterAdded fires, so the for loop could be setting the groups of a few or no parts.
You need to resolve this by actually checking both for new and existing parts and then configuring the collision group of those parts accordingly. Adding a wait before running code is never a proper solution; look to existing events and methods to solve this.
Use DescendantAdded on the character first and check if the passed descendant is a BasePart, then set its collision group accordingly. Afterwards run a for loop with GetDescendants on the character and check for BaseParts; if the current iteration is looking at a BasePart descendant, set its group.
I tried cloning and then I created a collide group from the physics service, but it still didn’t work. Also I couldn’t learn to use GetDescendants. @colbert2677"
It’s basically :GetChildren() but it gets completely every inside the model / instance. :GetDescendants() helps with accessories which have handles in them which are BasePart's but aren’t directly inside the model so :GetChildren() can’t get it.
Also you didn’t use :GetDescendants() in the player joined function so that might be one thing.
Also dev console is pretty clean.
By the way, there are mesh parts inside the characters, why is this a problem?
If I create a separate folder for them and weld them to the characters separately, will the problem be solved?
wait(0.5)
local PhysicsService = game:GetService("PhysicsService")
function setCollisionGroupRecursive(object)
if object:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(object, "NPCS")
end
for _, child in ipairs(object:GetChildren()) do
setCollisionGroupRecursive(child)
end
end
setCollisionGroupRecursive(script.Parent)
Server Script
local PhysicsService = game:GetService("PhysicsService")
PhysicsService:CreateCollisionGroup("NPCS")
PhysicsService:CollisionGroupSetCollidable("NPCS", "NPCS", false)
function setCollisionGroupRecursive(object)
if object:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(object, "NPCS")
end
for _, child in ipairs(object:GetChildren()) do
setCollisionGroupRecursive(child)
end
end
function onCharacterAdded(character)
wait(0.2)
setCollisionGroupRecursive(character)
end
function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharacterAdded)
end
game.Players.PlayerAdded:Connect(onPlayerAdded)