Disabling Collision on Cloned Npcs

Hello! I am attempting to disable collision on npcs however I encountered a quite difficult challenge, you see, my npcs are made via cloning so the script that I am using in serverscriptservice

local brickmin = workspace.Brickmin

local ps = game:GetService("PhysicsService")

ps:CreateCollisionGroup("PlayersGroup")
ps:CreateCollisionGroup("NpcsGroup")

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

	for _,npc in pairs(brickmin:GetChildren()) do
		for _,v in pairs(npc:GetDescendants()) do
			if v:IsA("BasePart") then
				ps:SetPartCollisionGroup(v,"NpcsGroup")
			end
		end
	end

	game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			wait()
			for _,v in pairs(char:GetChildren()) do
				if v:IsA("BasePart") then
					ps:SetPartCollisionGroup(v,"PlayersGroup")
				end
			end
		end)
	end)



doesn’t actually do anything since when the game is ran the brickmin folder is empty since no clones have been created, I just wanted to know if it was possible to maybe run this script when a clone is created or if there’s an alternative method I could use.

local function OnNPCAdded(npc)
   for _,v in pairs(npc:GetDescendants()) do
		if v:IsA("BasePart") then
			ps:SetPartCollisionGroup(v,"NpcsGroup")
		end
	end
end

for _,npc in pairs(brickmin:GetChildren()) do
	OnNPCAdded(npc)
end

brickmin.ChildAdded:Connect(OnNPCAdded)

Doesn’t work, they still have collision after being cloned, if it helps here is the cloning script.

local prompt = script.Parent.Head.Pluck
local brickType = script.Parent.Type.Value
local brickmin = game.ReplicatedStorage.Brickmin
local ps = game:GetService("PhysicsService")
local npcs = workspace.Brickmin

prompt.Triggered:Connect(function(plr)
	local hrp = plr.Character.HumanoidRootPart
	local clone = brickmin[brickType.."Brickmin"]:Clone()
	clone.Parent = game.Workspace.Brickmin
	clone:MoveTo(hrp.Position)
	clone.BelongsTo.Value = plr.Character.Name
	script.Parent:Destroy()
end)

Oh and the npcs being cloned are the same as player characters with HumanoidRootParts and stuff like that.

Shouldn’t it be:

ps:RegisterCollisionGroup("PlayersGroup")
ps:RegisterCollisionGroup("NpcsGroup")

You’re right, I have changed the script to fit that.

local brickmin = workspace.Brickmin

local ps = game:GetService("PhysicsService")

ps:RegisterCollisionGroup("PlayersGroup")
ps:RegisterCollisionGroup("NpcsGroup")

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

local function OnNPCAdded(npc)
	for _,v in pairs(npc:GetDescendants()) do
		if v:IsA("BasePart") then
			ps:SetPartCollisionGroup(v,"NpcsGroup")
		end
	end
end

for _,npc in pairs(brickmin:GetChildren()) do
	OnNPCAdded(npc)
end

brickmin.ChildAdded:Connect(OnNPCAdded)

brickmin.ChildAdded:Connect(function(npc)
	for _,v in pairs(npc:GetDescendants()) do
		if v:IsA("BasePart") then
			ps:SetPartCollisionGroup(v,"NpcsGroup")
		end
	end
end)


Btw, SetPartCollissionGroup is deprecated and doesn’t work, instead do this:

ps.CollisionGroup = "NpcsGroup"

Updated Code:

local brickmin = workspace.Brickmin

local ps = game:GetService("PhysicsService")

ps:RegisterCollisionGroup("PlayersGroup")
ps:RegisterCollisionGroup("NpcsGroup")

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

local function OnNPCAdded(npc)
	for _,v in pairs(npc:GetDescendants()) do
		if v:IsA("BasePart") then
			ps.CollisionGroup = "NpcsGroup"
		end
	end
end

for _,npc in pairs(brickmin:GetChildren()) do
	OnNPCAdded(npc)
end

brickmin.ChildAdded:Connect(OnNPCAdded)

brickmin.ChildAdded:Connect(function(npc)
	for _,v in pairs(npc:GetDescendants()) do
		if v:IsA("BasePart") then
			ps.CollisionGroup = "PlayersGroup"
		end
	end
end)

Throws an error

  02:07:08.663  CollisionGroup is not a valid member of PhysicsService "PhysicsService"  -  Server - NpcCollide:13

Mb,

local brickmin = workspace.Brickmin

local ps = game:GetService("PhysicsService")

ps:RegisterCollisionGroup("PlayersGroup")
ps:RegisterCollisionGroup("NpcsGroup")

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

local function OnNPCAdded(npc)
	for _,v in pairs(npc:GetDescendants()) do
		if v:IsA("BasePart") then
			v.CollisionGroup = "NpcsGroup"
		end
	end
end

for _,npc in pairs(brickmin:GetChildren()) do
	OnNPCAdded(npc)
end

brickmin.ChildAdded:Connect(OnNPCAdded)

brickmin.ChildAdded:Connect(function(npc)
	for _,v in pairs(npc:GetDescendants()) do
		if v:IsA("BasePart") then
			v.CollisionGroup = "PlayersGroup"
		end
	end
end)

Everything seems to be working now! Thanks a bunch!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.