Npc collide script not working properly

Hello devs!

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.

What is wrong? I hope I could explain.
Thanks!

Script:

Npcs: lol
image

1 Like

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.

Check Character Parts - Restricted/Team Doors for sample code and the reason why this is necessary.

6 Likes

Well thanks for your answer but I still couldn’t do it even though I really tried I guess I misunderstood something.

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"

Or any other help?

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.

So should I just use :GetDescendants() instead of :GetChildren()? Is that all

  • I know it’s hard to understand. My English is not very good.

In this case, yeah pretty much.

Turns out BasePart signifies any kind of part including MeshPart and Union

There might be parts inside the NPCS which aren’t a BasePart. Like Unions or MeshParts.

Still same
image
image

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?

I was mistaken that BaseParts are only roblox’s normal parts which don’t include MeshParts or Unions so that should be fine.

It seems you are using pairs instead of ipairs. Try changing them to ipairs and seeing if anything changes.

I’m not sure if that would change anything, as far as i know, pairs and ipairs just gets the objects inside a table in different orders.

Yep works the same in testing.

I can’t figure out what else could be causing this problem.

Try to paste the 5th line of code at the end and see if now it works

I tested the script and it works perfectly fine.
Might be the structure of the NPCS folder / group.

By “perfectly fine” you mean the collision groups now work?
or that the script still doesnt work?

Yeah I made an identical script and it worked.
Tested with different kinds of characters and it still worked.

1 Like

The script is already running in my test arena, namely the hollow baseplate. not working in main game

image

Alright after hours of effort thank you all for your help! :heartbeat: @colbert2677 @bnxDJ @marix8275

Here is the results;

Npc Script
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)
2 Likes