NPC can still be pushed by the players when spawned into the Workspace

I’m trying to make an NPC which cannot collide with the players, this way I can ensure that it will walk to it’s checkpoint without being pushed behind a wall by the players. Previously I tried to anchor the HumanoidRootPart or any part of the NPC, and while this does stop the NPC from being pushed around it also has a 999% chance to forcefully trip the NPC as soon as they get unanchored making them get flung off the map and defeating the purpose. So I tried many different methods using collision groups which also has a 50-50 chance of failing for some reason as well as making no sense to understand. So I just used some free model script that just so happens to work by keeping all the affected characters in a folder. When I have a character already existing in the folder it works but I’m trying to spawn it in the game from ReplicatedStorage, and when I do that it just doesn’t work. You can push the NPC around all just because I spawned it in the game after 10 seconds. In case you were wondering I tried this with a different script and I got the same result so idrc at this point, I’ll just give up and let someone else go through my unsolvable problem. If anyone wants to fix it then here is the uncopylocked place, you can take all the test assets in there if you want it but if you somehow manage to fix the bug then send me back the updated place. Thanks.

https://www.roblox.com/games/7467353640/the-NPC-that-wouldnt-move-out-of-the-way

Your code doesn’t account for when you add a new NPC to the NPC’s folder. It only accounts for NPCS that were in the folder when you press play.

I edited your code to fix this:

local npcs=workspace.NPCS
local ps=game:GetService("PhysicsService")

ps:CreateCollisionGroup("PlayersGroup")
ps:CreateCollisionGroup("NpcsGroup")
ps:CollisionGroupSetCollidable("PlayersGroup", "NpcsGroup", false)

function setCollisionGroup(model, collisionGroup)
	for _,v in pairs(model:GetDescendants()) do
		if v:IsA("BasePart") then
			ps:SetPartCollisionGroup(v,collisionGroup)
		end
	end
end

-- when an npc gets added to the npc folder,
-- they are added to the "NpcsGroup" collision group
npcs.ChildAdded:Connect(function(newChild)
	if(newChild:IsA("Model")) then
		setCollisionGroup(newChild, "NpcsGroup")
	end
end)

-- adds the npcs in the npc folder to the "NpcsGroup" collision group:
for _,npc in pairs(npcs:GetChildren()) do
	if(npc:IsA("Model")) then
		setCollisionGroup(npc, "NpcsGroup")
	end
end

--when a players character gets added to the game, they are put in the "PlayersGroup" collision group
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		setCollisionGroup(char, "PlayersGroup")
	end)
end)

This for some reason doesn’t work, both NPCs can now be pushed around

Did you make sure to delete everything that was in the collision script and then paste in the script I sent you?

I’m thinking maybe it was set up wrong because in the place file I am sending you, it works fine. Here’s a working version of the game: workingVersion.rbxl (41.9 KB)

Please take a look and let me know if you need any more help with it.

I did paste it in the collision script and I downloaded the place you sent me, both of them for some reason don’t work.

That’s a bit strange, this is the behaviour you want, right?

What’s not working about the place file?

The video is exactly how I want it to work, however it just doesn’t work on my end for some reason. Both characters can be pushed by the player.

https://gyazo.com/86774d2cbfcd8375cf9516bd290345b9

Can you show me a video of you opening the place file and the game not working?
Because it works for me everything when I open it like this:

Just remember that the place file I sent you won’t automatically update your place. Maybe we can team create and I can put the script directly into your game?

1 Like

Odd, I think it might be an issue with playeradded not firing in time. Maybe try reordering the events like so:

local npcs=workspace.NPCS
local ps=game:GetService("PhysicsService")

ps:CreateCollisionGroup("PlayersGroup")
ps:CreateCollisionGroup("NpcsGroup")
ps:CollisionGroupSetCollidable("PlayersGroup", "NpcsGroup", false)

function setCollisionGroup(model, collisionGroup)
	for _,v in pairs(model:GetDescendants()) do
		if v:IsA("BasePart") then
			ps:SetPartCollisionGroup(v,collisionGroup)
		end
	end
end

--when a players character gets added to the game, they are put in the "PlayersGroup" collision group
game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		setCollisionGroup(char, "PlayersGroup")
	end)
end)

-- when an npc gets added to the npc folder,
-- they are added to the "NpcsGroup" collision group
npcs.ChildAdded:Connect(function(newChild)
	if(newChild:IsA("Model")) then
		setCollisionGroup(newChild, "NpcsGroup")
	end
end)

-- adds the npcs in the npc folder to the "NpcsGroup" collision group:
for _,npc in pairs(npcs:GetChildren()) do
	if(npc:IsA("Model")) then
		setCollisionGroup(npc, "NpcsGroup")
	end
end


I gotta head to my job soon, I’ll try and mess around with it some more when I get home. Meanwhile maybe try and figure out what’s causing the issue.

1 Like

hope your day at your job goes well and i hope we get this sorted. if this doesn’t work, i’m not fully sure what it could be then… please keep me updated!!

It might be something with your character model or some game setting or other script interfering? I don’t know but I do like how you use the old mouse cursor!

This is indeed very odd. I would recommend putting invisible parts around the NPC, make sure that it’s anchored as well.

I don’t understand how it can just break even though someone clearly fixed my bug, but when they sent me the file of their place it just breaks for me like I’m being targeted by some sort of curse…

Humanoid automatically sets the collision of a rig.

Wait I think I fixed it. Since the script instantly sets everything from their original places I just moved the NPCS folder in replicated storage and when the game spawns the NPC from there it works.

Solved it. Instead of using .CharacterAdded(), use .CharacterAppearanceLoaded() instead. The script was breaking because .CharacterAdded() fires as soon as the character is parented to the workspace even if they do not have any limbs (ex UpperTorso, Head, LowerArm) whereas .CharacterAppearanceLoaded() waits for everything to be loaded in.

Nevermind, there’s a whole article about how to set collisions here: Detecting Collisions | Roblox Creator Documentation

You should follow that instead since it is pretty much the best resource you can use. Do not ever use .CharacterAppearanceLoaded() because there is always a chance that Roblox will encounter server errors and load characters as a blank character which will not fire the event.