So I was making this model move, and it worked. But, it only moves it one direction. I was wondering how I can make the part Go up, down, left right.
(I tried figuring it out by myself and nothing worked.)
local MP = script.Parent.MainPart
local Model = script.Parent
while true do
for i = 0,1,.001 do
Model:SetPrimaryPartCFrame(Model:GetPrimaryPartCFrame() * CFrame.new(0,9,0))
wait(.01)
end
end
To make it go left or right, you just change the first value, which is the x value, in the CFrame.new() to either a positive or a negative number. To make it go down, the y value (second value) needs to be negative.
Using the :MoveTo() method of either the model itself or humanoid (if the model was an NPC and it has a humanoid), you can simply make the model move wherever you want just by simply choosing the position and passing it to the function. You can do the same for you case by changing the CFrame.new(0,9,0) by another CFrame with any other position.
You could make your loop a function with a CFrame argument and have that loop in the function. Then call that function multiple times in your while true do loop with different CFrame values. What I first said was stupid…
You could make a variable outside all of the loops. In the for loop in the function, you could have an if statement checking if the value of that variable is true and if it is, then set it back to false and break the loop. When you call the function containing the loop, use coroutine.wrap() and give it that function, so that the script can continue to where it changes the direction. before wrapping the function again and giving the new CFrame, set the value of the check variable to true so that the loop that was started earlier stops.
Another, and probably easier solution would be to use TweenService. You could just use :Cancel and :Destroy() on the created tween Instance (using only destroy might be enough) before you make it move to another direction with a new tween.
Actually, maybe it would work…, because it actually is probably smooth because although it moves a lot, the wait time is small… I should have thought more about what I was saying before making that edit to my post.
You could have functions for each direction you want the model to move. Such as “moveForward()”, “moveBackward()”, “moveLeft()”, and “moveRight()”. Then have boolValues outside of those functions. Then, in the functions, have a “while bool = true do” for each of the functions. And also have the function change the other bool values to false to disable the movement. Then just call the function when you want to move the model into a different direction. Something like this:
local moveForwardBool = false
local moveBackwardBool = false
local moveLeftBool = false
local moveRightBool = false
local function moveForward()
moveBackwardBool = false
moveLeftBool = false
moveRightBool = false
moveForwardBool = true
while moveForwardBool = true do
-- Code for model to move forward
end
end
local function moveBackward()
moveForwardBool = false
moveLeftBool = false
moveRightBool = false
moveBackwardBool = true
while moveBackwardBool = true do
-- Code for model to move backward
end
end
local function moveLeft()
moveBackwardBool = true
moveForwardBool = false
moveRightBool = false
moveLeftBool = true
while moveLeftBool = true do
-- Code for model to move left
end
end
local function moveRight()
moveBackwardBool = true
moveForwardBool = false
moveLeftBool = false
moveRightBool = true
while moveRightBool = true do
-- Code for model to move right
end
end
When you want it to move forward, let’s say, just do:
moveForward()
It should stop moving in any other direction and just go in the direction specified.