My npc ai script is not working

Hello my roblox ai script is not working im using bones inside a meshpart
when i paste the ai script on annother humanoid it works but with bones it doesn’t work anyone know how to fix this?

image

image

local npc = script.Parent
local Humanoid = npc:WaitForChild("Humanoid")

for i, v in pairs(npc:GetChildren()) do
	if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("MeshPart.Bone") then
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				hit.Parent.Humanoid.Health -= 50
			end
		end)
	end
end

local function GetClosestPlayer()
	local MinimumDistance = math.huge
	
	local ClosestPlayer = nil
	
	for i,v in pairs(game.Players:GetChildren()) do
		local Character = v.Character
		if Character then
			local Distance = (npc.PrimaryPart.Position - Character.PrimaryPart.Position).Magnitude
			
			if Distance < MinimumDistance then
				MinimumDistance = Distance
				ClosestPlayer = v
			end
		end
	end
	return ClosestPlayer
end

while wait(0.5) do
	local ClosestPlayer = GetClosestPlayer()
	
	if ClosestPlayer then
		local TargetPosition = ClosestPlayer.Character.PrimaryPart.Position
		Humanoid:MoveTo(TargetPosition)
	end
end
3 Likes

Do you mind sending me the experience through PM’s. So I can see if it’s anchored, or if there are outside factors, like the rig being anchored, etc.

1 Like

Ai.rbxl (55.0 KB)
sure