Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make it so players cant collide with NPCs?

I've tried to just anchor the hrp like some people suggest and it broke the animation, i also tried physics service, heres my script

Player.CharacterAdded:Connect(function(Char)		
		coroutine.resume(coroutine.create(function()
			for _,v in pairs(workspace.Workspace.QuestNPCs:GetDescendants()) do
				if v:IsA("BasePart") then
					PhysicsService:CreateCollisionGroup("QuestNPC")	
					PhysicsService:SetPartCollisionGroup(v, "QuestNPC")
				end
			end
			for _,v in pairs(Char:GetChildren()) do
				if v:IsA("BasePart") then
					PhysicsService:CreateCollisionGroup("Players")
					PhysicsService:SetPartCollisionGroup(v, "Players")		
				end
			end
			PhysicsService:CollisionGroupSetCollidable("QuestNPC", "Players", false)
		end))
end)

1 comment

0

It also doesn't work btw

1 answer

✓ Accepted answer

You are using PhysicsService a bit wrong, here i made a working example of how you could implement it

local PhysicsService = game:GetService('PhysicsService')
local Players = game:GetService('Players')

PhysicsService:CreateCollisionGroup('Players')
PhysicsService:CreateCollisionGroup('NPCs')
PhysicsService:CollisionGroupSetCollidable('Players', 'NPCs', false)

local NPC = workspace.Model

local function SetModelGroup(Model, Group)
	
	for _, Part in ipairs(Model:GetChildren())
	do
		if (Part:IsA 'BasePart') then
		
			PhysicsService:SetPartCollisionGroup(Part, Group)
		end
	end
end

local function OnClientAdded(Client)
	
	local function OnCharacterLoaded(Character)
		
		SetModelGroup(Character, 'Players')
	end
	
	Client.CharacterAppearanceLoaded:Connect(OnCharacterLoaded)
end

SetModelGroup(NPC, 'NPCs')

Players.PlayerAdded:Connect(OnClientAdded)

In my case it loops through children although you can make it loop through GetDescendants if need it, for players it is unnecessary.

The script creates 2 collision groups, NPCs and Players, they are then set to not be collideable. You don't need to create new collision groups for every itteration in a loop as you did, it's redundant.

You can call SetModelGroup on any model, second parameter is name of the collision group.

I strongly suggest using CharacterAppearanceLoaded event which is similar to CharacterAdded but it fires when your accessories are loaded, in this case there is a lot higher chance that body parts are going to be loaded.

If you are having problem that some parts are not yet replicated, you can try using DescendantAdded event on the character and set collision group for invididual parts of the character when they replicate.

Here are all the sources i used

1 comment

0

Thank you

This discussion is preserved as it was. New answers, comments, and votes are closed.