Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I flip a car upright properly?

In my opinion, BodyMovers are confusing. I'm trying to make a script that flips a car upright whenever you press V. However, it's not doing that, and making the car turn erratically instead. I'm using a BodyGyro to flip the car by the way. Here is my script:

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local UserInputService = game:GetService("UserInputService")
local inCar = false
local vehicle

local function onSit(Sitting, seat)
	if Sitting == true then
		if seat.Name == "DriveSeat" then
			inCar = true
			vehicle = seat.Parent.Parent
			camera.FieldOfView = 100
			repeat wait()
				camera.CameraType = Enum.CameraType.Attach
			until camera.CameraType == Enum.CameraType.Attach
		end
	else
		inCar = false
		camera.FieldOfView = 70
		repeat wait()
			camera.CameraType = Enum.CameraType.Custom
		until camera.CameraType == Enum.CameraType.Custom
	end
end

local function flipCar(car)
	if inCar then
		local yRot = car:FindFirstChild("Car Weld").DriveSeat.Orientation.Y
		local seat = car:FindFirstChild("Car Weld").DriveSeat
		
		seat:FindFirstChild("BodyGyro").CFrame = CFrame.fromOrientation(0, -1*yRot, 0)
	end
end

local function flip(Input)
	if Input.KeyCode == Enum.KeyCode.V then
		flipCar(vehicle)
	end
end

humanoid.Seated:Connect(onSit)
UserInputService.InputBegan:Connect(flip)

Any help would be appreciated.

1 answer

✓ Accepted answer

Hope this helps!

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local UserInputService = game:GetService("UserInputService")
local inCar = false
local vehicle

local function onSit(Sitting, seat)
    if Sitting == true then
        if seat.Name == "DriveSeat" then
            inCar = true
            vehicle = seat.Parent.Parent
            camera.FieldOfView = 100
            repeat wait()
                camera.CameraType = Enum.CameraType.Attach
            until camera.CameraType == Enum.CameraType.Attach
        end
    else
        inCar = false
        camera.FieldOfView = 70
        repeat wait()
            camera.CameraType = Enum.CameraType.Custom
        until camera.CameraType == Enum.CameraType.Custom
    end
end

local function flipCar(car)
    if inCar then
        local yRot = car:FindFirstChild("Car Weld").DriveSeat.Orientation.Y
        local seat = car:FindFirstChild("Car Weld").DriveSeat

        repeat wait() seat:FindFirstChild("BodyGyro").MaxTorque = Vector3.new(1000,0,1000) until seat.CFrame.upVector.Y < 0.3 --Mess with the 0.3 for your car.
    end
end

local function flip(Input)
    if Input.KeyCode == Enum.KeyCode.V then
        flipCar(vehicle)
    end
end

humanoid.Seated:Connect(onSit)
UserInputService.InputBegan:Connect(flip)

2 comments

0

Wow thanks, it worked. Theres just one problem, now the car is not moving in the right direction, and when I try to set the Body Gyro's max torque to zero, it did nothing. How can I counter this?

0

Nevermind, I managed to fix this problem by making the script temporarily add a Body Gyro, then removing it after it did its job, but thank you for helping me with the flipping part!

This discussion is preserved as it was. New answers, comments, and votes are closed.