I’m trying to make a script to constantly check the car’s orientation to see if the car flips and if it does then it would orient itself up.
This is what I’ve tried:
local RunService = game:GetService("RunService")
RunService.Stepped:Connect(function()
if script.Parent.VehicleSeat.Orientation.X > 80 or script.Parent.VehicleSeat.Orientation.X < -80 then
script.Parent.VehicleSeat.Orientation.X = 0
end
if script.Parent.VehicleSeat.Orientation.Z > 80 or script.Parent.VehicleSeat.Orientation.Z < -80 then
script.Parent.VehicleSeat.Orientation.Z = 0
end
end)
I wasn’t too sure if there was a Vector3.new involved but it didn’t work when I tried…
Vector3’s are immutable, which means they can’t be changed, the individual axes are read-only therefore can’t be set to anything directly. So you’ll want something like
local MAX_X = 80
local MIN_X = -MAX_X
local MAX_Z = 80
local MIN_Z = -MIN_Z
local seat = script.Parent:WaitForChild("VehicleSeat")
RunService.Stepped:Connect(function()
local current_orientation = seat.Orientation
seat.Orientation = Vector3.new(
math.clamp(current_orientation.X, MIN_X, MAX_X),
current_orientation.Y,
math.clamp(current_orientation.Z, MIN_Z, MAX_Z)
)
end)
this will keep it constrained, since math.clamp(x, min, max) returns min if x < min otherwise max if x > max, otherwisw if x is within the defined bounds, then x is just returned