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.
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)
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.
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?
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
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!
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…
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.
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.