Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to change velocity in bodyvelocity?

Asked by 9 years ago

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
1
Use Vector3.new to declare the velocity as it is a vector3 value :3 e.g script.Parent.Parent.M1.BodyVelocity.velocity = Vector3.new(0,0,2) Ryzox 220 — 9y
0
Ok and can you tell me how to repeat it? i mean restart or something. DevKarolus1 70 — 9y

3 answers

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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
0
How to repeat it? becouse after last change it will only move in one direction. DevKarolus1 70 — 9y
0
Also, if you're trying to move it back and forth between two points, use BodyPosition instead. Otherwise, this is fine. Perci1 4988 — 9y
0
Thanks for help and explain how it works. DevKarolus1 70 — 9y
0
Can i still ask you something ? This part hanging in the air so how to make it stable? becouse it falling and when i put it anchored don't work bodyvelocity. DevKarolus1 70 — 9y
View all comments (2 more)
0
So it looks like you do need to use BodyPosition. http://wiki.roblox.com/index.php?title=BodyPosition Also, here's a video roblox made that's very helpful with this topic: https://www.youtube.com/watch?v=dqyJk0w2AY0 Perci1 4988 — 9y
0
Ok thanks again. DevKarolus1 70 — 9y
Ad
Log in to vote
1
Answered by 9 years ago

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
0
And how to repeat it? DevKarolus1 70 — 9y
0
Thanks for help. DevKarolus1 70 — 9y
0
No problem :) YellowoTide 1992 — 9y
Log in to vote
0
Answered by
iSvenDerp 233 Moderation Voter
9 years ago

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:)

0
Thanks for help. DevKarolus1 70 — 9y

Answer this question