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

How to change body velocity with a script?

Asked by
hokyboy 270 Moderation Voter
3 years ago
Edited by Rare_tendo 3 years ago
script.Parent.Touched:Connect(function(V)
    if V.Parent:FindFirstChild("Trian") then
        print("TrainWasFound")
        V.Parent.Main.BodyVelocity.Velocity.X = -5
    end
end)

1 answer

Log in to vote
1
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

The positional components of a Vector3 are immutable, meaning that you can't edit the properties directly or it'll return an error. You will have to create a new vector with the new X component:

script.Parent.Touched:Connect(function(V)
    if V.Parent:FindFirstChild("Train") then
        print("TrainWasFound")
        local vel = v.Parent.Main.BodyVelocity.Velocity
        V.Parent.Main.BodyVelocity.Velocity = Vector3.new(-5, vel.Y, vel.Z)
    end
end)

At least try to include some context of the issue you're having, how you intended to behave and any error(s) in your questions

Ad

Answer this question