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?
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