How do you make something roam using BodyVelocity?

I want to know how to make something randomly move around or “roam”, preferably with body velocity or something of the like, but I don’t know how since I’m relatively new to scripting and really new to body velocity and the others. It works fine until it gets to the part where it has to change directions after waiting a certain amount of time.
Here’s what I have:

local x = math.random (-5,5)
local y = math.random (-5,5)
local time = math.random (10,30)

while true do
script.Parent.Velocity = Vector3.new (x, 0, y)
wait(time)
end

Can someone help me figure out what I did wrong? Also, this is my first post on here so if I formatted it wrong or something, please tell me so I can fix it in the future.

You are extremely close!

All you need to do, is put the randomness inside of the while loop, so that the time and direction are recalculated when the loop repeats.

while true do
    local x = math.random (-5,5)
    local y = math.random (-5,5)
    local time = math.random (10,30)

    script.Parent.Velocity = Vector3.new (x, 0, y)
    wait(time)
end
2 Likes

Thank you so much! I was stuck on this for a few hours now but I got it to work

1 Like

Hey, dude. I hate to bother you again but I have no idea how to make the aforementioned roaming model face the way it’s moving. I’ve tried looking it up but none of the other answers were exactly what I was looking for. Could you help me out again?

Before moving the part, you could set its CFrame to look at the direction it is going to go, using the LookAt Vector property of CFrame.new.

while true do
    local x = math.random (-5,5)
    local y = math.random (-5,5)
    local time = math.random (10,30)

    script.Parent.CFrame = CFrame.new(script.Parent.Position, Vector3.new(x, 0, y) + script.Parent.Position)
    script.Parent.Velocity = Vector3.new (x, 0, y)
    wait(time)
end
1 Like

Thank you so much once again! It worked!

1 Like