0

I'm trying to have a model move around and jump using this code but the object does not move. (everything prints until it gets to the movement part.) The model just stays stationary and can be affacted by the player like an unancored object.

    -- Variables
local mob = script.Parent
local humanoid = mob:FindFirstChildOfClass("Humanoid")
local runService = game:GetService("RunService")
local jumpHeight = 50
local moveInterval = 2 -- Time in seconds between movements

-- Debugging prints
print("Script is running")
if humanoid then
    print("Humanoid found:", humanoid)
else
    warn("Humanoid not found in the model")
end

-- Ensure PrimaryPart is set
if not mob.PrimaryPart then
    warn("PrimaryPart is not set in the model. Please set the PrimaryPart.")
    return
end

-- Function to move the mob randomly
local function moveRandomly()
    local randomX = math.random(-50, 50)
    local randomZ = math.random(-50, 50)
    local targetPosition = mob.PrimaryPart.Position + Vector3.new(randomX, 0, randomZ)

    print("Target position:", targetPosition) -- Debug print

    -- Move mob to target position
    if humanoid then
        humanoid:MoveTo(targetPosition)

        print("Humanoid:MoveTo called") -- Debug print

        -- Make the mob jump after reaching the position
        humanoid.MoveToFinished:Connect(function(reached)
            if reached then
                print("Reached target position")
                humanoid.Jump = true
                print("Jumping at position:", targetPosition) -- Debug print
            else
                print("Did not reach target position")
            end
        end)
    else
        warn("Humanoid is missing") -- Debug print
    end
end

-- Loop to continuously move the mob
while true do
    if humanoid and mob.PrimaryPart then
        moveRandomly()
        wait(moveInterval)
    else
        print("Humanoid or PrimaryPart missing") -- Debug print
        break
    end
end

Here is my test model setup. enter image description here

0

Browse other questions tagged or ask your own question.