Hello, Im trying to change velocity in bodyvelocity which is in part. I want to make part which moving right for 3 sec and later it change and move to left.
Here is script:
script.Parent.Parent.M1.BodyVelocity.velocity.X = 0 script.Parent.Parent.M1.BodyVelocity.velocity.Y = 0 script.Parent.Parent.M1.BodyVelocity.velocity.Z = 2 wait(3) script.Parent.Parent.M1.BodyVelocity.velocity.X = 0 script.Parent.Parent.M1.BodyVelocity.velocity.Y = 0 script.Parent.Parent.M1.BodyVelocity.velocity.Z = -2
The X
, Y
, and Z
properties of Vector3 are read-only, meaning you can't edit their values. But the velocity
property itself isn't read-only, so we can edit that. It is, however, a Vector3 value, so we have to use Vector3.new
.
script.Parent.Parent.M1.BodyVelocity.velocity = Vector3.new(0, 0, 2) wait(3) script.Parent.Parent.M1.BodyVelocity.velocity = Vector3.new(0, 0, -2)
To keep this repeating, we must put it in a loop. The while
loop will suffice. This loop takes the form of,
while (condition) do end
with the loop only running if condition
equals true. Since we want it to repeat forever, we can just use the word true
, since true
will never equal false.
while true do script.Parent.Parent.M1.BodyVelocity.velocity = Vector3.new(0, 0, 2) wait(3) script.Parent.Parent.M1.BodyVelocity.velocity = Vector3.new(0, 0, -2) wait(3) end
You can't set individual properties of a Vector3 by themselves, you have to set them all at once with a Vector3
It's also a good habit to declare variables
local bodyVelocity = script.Parent.Parent.M1.BodyVelocity bodyVelocity.velocity = Vector3.new(0, 0, 2) wait(3) bodyVelocity.velocity = Vector3.new(0, 0, -2)
To repeat it you would use a 'while' loop
while true do print("hello :c") wait() end
That will repeat forever printing hello :c
We can put your code in a while loop and it'll repeat forever!
local bodyVelocity = script.Parent.Parent.M1.BodyVelocity while true do bodyVelocity.velocity = Vector3.new(0, 0, 2) wait(3) bodyVelocity.velocity = Vector3.new(0, 0, -2) wait(3) --We need another wait here or else it's going to set it to '2' right away end
In the comments you were asking how to repeat it so if you want to repeat it over and over again do this
local bodyVelocity = script.Parent.Parent.M1.BodyVelocity -- This is making the variable while true do-- This is a loop that makes it repeat over and over bodyVelocity.velocity = Vector3.new(0, 0, 2) wait(3)-- This is waiting for it to repeat each time bodyVelocity.velocity = Vector3.new(0, 0, -2) end
Cant Test it right now but im pretty sure it works so hope this helpes:)