#npc not moving with the animation i inserted into the script

1 messages · Page 1 of 1 (latest)

dusky sable
#

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local chucky = script.Parent
local humanoid = chucky:WaitForChild("Humanoid")
local hrp = chucky:WaitForChild("HumanoidRootPart")

humanoid.WalkSpeed = 16

local RUN_ANIM_ID = "rbxassetid://616163682"
local animator = humanoid:FindFirstChildOfClass("Animator") or Instance.new("Animator", humanoid)

local runAnim = Instance.new("Animation")
runAnim.AnimationId = RUN_ANIM_ID
local runTrack = animator:LoadAnimation(runAnim)

local lastPos = hrp.Position
local stuckTime = 0
local jumpDelay = 1

local function getNearestPlayer()
local closest
local minDist = math.huge

for _, player in ipairs(Players:GetPlayers()) do
    local char = player.Character
    if char and char:FindFirstChild("HumanoidRootPart") then
        local dist = (hrp.Position - char.HumanoidRootPart.Position).Magnitude
        if dist < minDist then
            minDist = dist
            closest = char
        end
    end
end

return closest

end

RunService.Heartbeat:Connect(function(dt)
local target = getNearestPlayer()
if not target then return end

local targetPos = target.HumanoidRootPart.Position
humanoid:MoveTo(targetPos)

local moved = (hrp.Position - lastPos).Magnitude > 0.1

if moved then
    lastPos = hrp.Position
    stuckTime = 0
    if not runTrack.IsPlaying then
        runTrack:Play()
    end
else
    stuckTime += dt
    if stuckTime >= jumpDelay then
        humanoid.Jump = true
        stuckTime = 0
    end
    if runTrack.IsPlaying then
        runTrack:Stop()
    end
end

end)