local K=false local D=false local C,s,N,w,P,M=Vector3.new,CFrame.new,math.rad,math.pi,CFrame.Angles,Ray.new local function n(K,D)D(K)for K,C in next,K:GetChildren()do n(C,D)end end function Hopoff()if SegwayObject.Value~=nil and(K==false and(SegwayObject.Value.PrimaryPart and SeaterObject.Value~=nil))then K=true D=false local N=SegwayObject.Value.PrimaryPart local w,P,M,n,d,F,c,v,U,B,W,o=N.CFrame:components()if SeaterObject.Value~=nil then repeat wait()until DeleteWelds:InvokeServer(SeaterObject.Value.Parent)==true or SeaterObject.Value==nil end Camera.CameraType="\067\117\115\116\111\109"if Thruster.Value then Thruster.Value.BodyGyro.MaxTorque=C(400000,0,400000)end if SeaterObject.Value and SeaterObject.Value.Parent:FindFirstChild("\082\117\110")then StopSound:FireServer(SeaterObject.Value.Parent.Run,0)PlaySound:FireServer(SeaterObject.Value.Parent.Off,SeaterObject.Value.Parent.Off.Pitch,SeaterObject.Value.Parent.Off.Volume)end if SegwayObject.Value then SegwayObject.Value:SetPrimaryPartCFrame(s(w,P+.1,M,1,d,F,c,1,U,B,W,1))end ConfigLights:FireServer(.3,"\066\114\105\103\104\116 \098\108\117\105\115\104 \103\114\101\101\110",false,"\083\109\111\111\116\104\080\108\097\115\116\105\099",Lights,Notifiers)GuiControls.Parent=script.Parent UserInputService.ModalEnabled=false OffButton.ImageColor3=LiftedColor if UserInputService.VREnabled then if PlayerGui and(PlayerGui:FindFirstChild("\068\105\115\112\108\097\121\071\117\105")and PlayerGui:FindFirstChild("\067\111\108\111\114\071\117\105"))then(PlayerGui:FindFirstChild("\068\105\115\112\108\097\121\071\117\105")).Enabled=true;(PlayerGui:FindFirstChild("\067\111\108\111\114\071\117\105")).Enabled=true end UpdateCharTrans(0,0)end SitListener:disconnect()JumpListener:disconnect()if TiltMotor.Value then TiltMotor.Value.DesiredAngle=0 end AnchorSegway:FireServer(N,true)UndoTags:FireServer(SegwayObject,WeldObject,TiltMotor)ConfigHumanoid:FireServer(Humanoid,false,true,true)wait(1)AnchorSegway:FireServer(N,false)UndoHasWelded:FireServer(SeaterObject)Direction=0 Steer=0 K=false end end function UpdateCharTrans(K,D)n(Character,function(C)if C and C:IsA("\066\097\115\101\080\097\114\116")then if C.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")or C.Parent:IsA("\065\099\099\101\115\115\111\114\121")then C.LocalTransparencyModifier=K else C.LocalTransparencyModifier=D end elseif C and(C:IsA("\068\101\099\097\108")and C.Name=="\102\097\099\101")then C.Transparency=K end end)end function UpdateVRPos()if UserInputService.VREnabled and(Character and(Character.Parent and Character:FindFirstChild("\072\101\097\100")))then local K,D,N,w,P,M,n,d,F,c,v,U=Character.Head.CFrame:components()Camera.CFrame=s(K,D,N,w,0,M,0,1,0,c,0,U)+C(P,d,v)*2.5 end end function Accelerate()if D then local K=(Direction*Thruster.Value.CFrame.lookVector)*MaxSpeed.Value local D=Thruster.Value.Velocity:Lerp(K,.05)local s=C(0,(-Steer*TurnSpeed.Value)/10,0)local N,w,P,M,n,d,F,c,v,U,B,W=Thruster.Value.CFrame:components()if c>.7 then Thruster.Value.BodyGyro.MaxTorque=C(400000,0,400000)Thruster.Value.Velocity=D Thruster.Value.RotVelocity=s else Thruster.Value.BodyGyro.MaxTorque=C(0,0,0)end end end function CastToGround()if Thruster.Value and Thruster.Value.Parent then local K,D,N,n,d,F,c,v,U,B,W,o=Thruster.Value.CFrame:components()local r=-C(d,v,W)local f=M(Thruster.Value.CFrame.p,r*50)local m,u,E=workspace:FindPartOnRayWithIgnoreList(f,{SegwayObject.Value,RayTest})local e=(Thruster.Value.Position-u).magnitude local z=(E-r).magnitude if m and(m.CanCollide and z>1.7)then Thruster.Value.BodyGyro.MaxTorque=C(400000,0,400000)Thruster.Value.BodyGyro.CFrame=s(u,u+E)*P(-w/2,0,0)end end end function Sound()if Thruster.Value and.85+N(Thruster.Value.Velocity.Magnitude/1.8)<=RunSoundPitchLimit then SeaterObject.Value.Parent.Run.Pitch=.85+N(Thruster.Value.Velocity.Magnitude/1.8)end end function Tilt()if Direction~=0 then TiltMotor.Value.DesiredAngle=Direction/10 SeaterObject.Value.Parent.Run.Volume=RunSoundVol else TiltMotor.Value.DesiredAngle=0 if Steer==0 then SeaterObject.Value.Parent.Run.Volume=0 else SeaterObject.Value.Parent.Run.Volume=RunSoundVol SeaterObject.Value.Parent.Run.Pitch=.85+N(Thruster.Value.RotVelocity.Magnitude/1.8)end end end function SetGuiButtons()OffButton.MouseButton1Down:connect(function()OffButton.ImageColor3=PressedColor end)OffButton.MouseButton1Up:connect(function()OffButton.ImageColor3=LiftedColor Hopoff(Character)end)OffButton.MouseLeave:connect(function()OffButton.ImageColor3=LiftedColor end)LeftButton.MouseButton1Down:connect(function()Steer=-1 LeftButton.ImageColor3=PressedColor end)LeftButton.MouseButton1Up:connect(function()Steer=0 LeftButton.ImageColor3=LiftedColor end)LeftButton.MouseLeave:connect(function()LeftButton.ImageColor3=LiftedColor end)RightButton.MouseButton1Down:connect(function()Steer=1 RightButton.ImageColor3=PressedColor end)RightButton.MouseButton1Up:connect(function()Steer=0 RightButton.ImageColor3=LiftedColor end)RightButton.MouseLeave:connect(function()RightButton.ImageColor3=LiftedColor end)DownButton.MouseButton1Down:connect(function()Direction=-1 DownButton.ImageColor3=PressedColor end)DownButton.MouseButton1Up:connect(function()Direction=0 DownButton.ImageColor3=LiftedColor end)DownButton.MouseLeave:connect(function()DownButton.ImageColor3=LiftedColor end)UpButton.MouseButton1Down:connect(function()Direction=1 UpButton.ImageColor3=PressedColor end)UpButton.MouseButton1Up:connect(function()Direction=0 UpButton.ImageColor3=LiftedColor end)UpButton.MouseLeave:connect(function()UpButton.ImageColor3=LiftedColor end)end
-- Other
local IsGettingOff = false
local IsOnSegway = false
local Vector3new, CFramenew, Mathrad, Mathpi, CFrameangles, Raynew =
	Vector3.new, CFrame.new, math.rad, math.pi, CFrame.Angles, Ray.new

local function Get_des(instance, func)
	func(instance)
	for _, child in next, instance:GetChildren() do
		Get_des(child, func)
	end
end

function Hopoff()
	if
		SegwayObject.Value ~= nil
		and IsGettingOff == false
		and SegwayObject.Value.PrimaryPart
		and SeaterObject.Value ~= nil
	then
		IsGettingOff = true
		IsOnSegway = false
		local PrimaryPart = SegwayObject.Value.PrimaryPart
		local px, py, pz, xx, yx, zx, xy, yy, zy, xz, yz, zz = PrimaryPart.CFrame:components()

		-- Delete welds
		if SeaterObject.Value ~= nil then
			repeat
				wait()
			until DeleteWelds:InvokeServer(SeaterObject.Value.Parent) == true or SeaterObject.Value == nil
		end

		-- Resets camera's behavior
		Camera.CameraType = "Custom"

		-- Set gyro to have power
		if Thruster.Value then
			Thruster.Value.BodyGyro.MaxTorque = Vector3new(400000, 0, 400000)
		end

		-- Stops sounds
		if SeaterObject.Value and SeaterObject.Value.Parent:FindFirstChild("Run") then
			StopSound:FireServer(SeaterObject.Value.Parent.Run, 0)
			PlaySound:FireServer(
				SeaterObject.Value.Parent.Off,
				SeaterObject.Value.Parent.Off.Pitch,
				SeaterObject.Value.Parent.Off.Volume
			)
		end

		-- Flips segway if fell over
		if SegwayObject.Value then
			SegwayObject.Value:SetPrimaryPartCFrame(CFramenew(px, py + 0.1, pz, 1, yx, zx, xy, 1, zy, xz, yz, 1))
		end

		-- Turn off lights
		ConfigLights:FireServer(0.3, "Bright bluish green", false, "SmoothPlastic", Lights, Notifiers)

		-- Re-parents mobile gui and reshows mobile controls
		GuiControls.Parent = script.Parent
		UserInputService.ModalEnabled = false
		OffButton.ImageColor3 = LiftedColor

		-- Shows tool UI via use with VR
		if UserInputService.VREnabled then
			if PlayerGui and PlayerGui:FindFirstChild("DisplayGui") and PlayerGui:FindFirstChild("ColorGui") then
				PlayerGui:FindFirstChild("DisplayGui").Enabled = true
				PlayerGui:FindFirstChild("ColorGui").Enabled = true
			end

			UpdateCharTrans(0, 0)
		end

		-- Disconnect events
		SitListener:disconnect()
		JumpListener:disconnect()

		-- Resets the tilt motor angle
		if TiltMotor.Value then
			TiltMotor.Value.DesiredAngle = 0
		end

		-- Makes the thruster's velocity still
		AnchorSegway:FireServer(PrimaryPart, true)

		-- Makes object values nil
		UndoTags:FireServer(SegwayObject, WeldObject, TiltMotor)

		-- Configures humanoid
		ConfigHumanoid:FireServer(Humanoid, false, true, true)

		-- Unanchors segway
		wait(1)
		AnchorSegway:FireServer(PrimaryPart, false)
		UndoHasWelded:FireServer(SeaterObject)

		-- Reset direction and steer tags
		Direction = 0
		Steer = 0
		IsGettingOff = false
	end
end

function UpdateCharTrans(BodyTrans, SegwayTrans)
	-- Changes character's body while VR
	Get_des(Character, function(d)
		if d and d:IsA("BasePart") then
			if d.Parent:FindFirstChild("Humanoid") or d.Parent:IsA("Accessory") then
				d.LocalTransparencyModifier = BodyTrans
			else
				d.LocalTransparencyModifier = SegwayTrans
			end
		elseif d and d:IsA("Decal") and d.Name == "face" then
			d.Transparency = BodyTrans
		end
	end)
end

function UpdateVRPos()
	if UserInputService.VREnabled and Character and Character.Parent and Character:FindFirstChild("Head") then
		local hpx, hpy, hpz, hxx, hxy, hxz, hyx, hyy, hyz, hzx, hzy, hzz = Character.Head.CFrame:components()

		Camera.CFrame = CFramenew(hpx, hpy, hpz, hxx, 0, hxz, 0, 1, 0, hzx, 0, hzz) + (Vector3new(hxy, hyy, hzy) * 2.5) --Height in studs above character's head
	end
end

function Accelerate()
	if IsOnSegway then
		local v = Direction * Thruster.Value.CFrame.lookVector * MaxSpeed.Value
		local NewVelocity = Thruster.Value.Velocity:Lerp(v, 0.05) -- Thrust
		local NewRotVelocity = Vector3new(0, -Steer * TurnSpeed.Value / 10, 0) -- Steering
		local px, py, pz, xx, xy, xz, yx, yy, yz, zx, zy, zz = Thruster.Value.CFrame:components()

		if yy > 0.7 then -- If segway is upsided
			Thruster.Value.BodyGyro.MaxTorque = Vector3new(400000, 0, 400000)
			Thruster.Value.Velocity = NewVelocity
			Thruster.Value.RotVelocity = NewRotVelocity
		else
			Thruster.Value.BodyGyro.MaxTorque = Vector3new(0, 0, 0)
		end
	end
end

function CastToGround()
	if Thruster.Value and Thruster.Value.Parent then
		-- Raycast to face segway towards ground
		local px, py, pz, xx, xy, xz, yx, yy, yz, zx, zy, zz = Thruster.Value.CFrame:components()
		local SegDnVect = -Vector3new(xy, yy, zy)
		local Ray = Raynew(Thruster.Value.CFrame.p, SegDnVect * 50)
		local hit, position, normal = workspace:FindPartOnRayWithIgnoreList(Ray, { SegwayObject.Value, RayTest })
		local HitDepth = (Thruster.Value.Position - position).magnitude
		local NextRayAngle = (normal - SegDnVect).magnitude

		if hit and hit.CanCollide and NextRayAngle > 1.7 then
			Thruster.Value.BodyGyro.MaxTorque = Vector3new(400000, 0, 400000)
			Thruster.Value.BodyGyro.CFrame = CFramenew(position, position + normal) * CFrameangles(-Mathpi / 2, 0, 0)
		end
	end
end

function Sound()
	if Thruster.Value and 0.85 + Mathrad(Thruster.Value.Velocity.Magnitude / 1.8) <= RunSoundPitchLimit then
		SeaterObject.Value.Parent.Run.Pitch = 0.85 + Mathrad(Thruster.Value.Velocity.Magnitude / 1.8)
	end
end

function Tilt()
	if Direction ~= 0 then
		TiltMotor.Value.DesiredAngle = Direction / 10
		SeaterObject.Value.Parent.Run.Volume = RunSoundVol
	else
		TiltMotor.Value.DesiredAngle = 0

		-- Change sound based on current status
		if Steer == 0 then
			SeaterObject.Value.Parent.Run.Volume = 0
		else --Adjusts the run sound when still and just turning
			SeaterObject.Value.Parent.Run.Volume = RunSoundVol
			SeaterObject.Value.Parent.Run.Pitch = 0.85 + Mathrad(Thruster.Value.RotVelocity.Magnitude / 1.8)
		end
	end
end

function SetGuiButtons()
	--Exit button
	OffButton.MouseButton1Down:connect(function()
		OffButton.ImageColor3 = PressedColor
	end)
	OffButton.MouseButton1Up:connect(function()
		OffButton.ImageColor3 = LiftedColor
		Hopoff(Character)
	end)
	OffButton.MouseLeave:connect(function()
		OffButton.ImageColor3 = LiftedColor
	end)

	--Left button pressed/lifted
	LeftButton.MouseButton1Down:connect(function()
		Steer = -1
		LeftButton.ImageColor3 = PressedColor
	end)
	LeftButton.MouseButton1Up:connect(function()
		Steer = 0
		LeftButton.ImageColor3 = LiftedColor
	end)
	LeftButton.MouseLeave:connect(function()
		LeftButton.ImageColor3 = LiftedColor
	end)

	--Right button pressed/lifted
	RightButton.MouseButton1Down:connect(function()
		Steer = 1
		RightButton.ImageColor3 = PressedColor
	end)
	RightButton.MouseButton1Up:connect(function()
		Steer = 0
		RightButton.ImageColor3 = LiftedColor
	end)
	RightButton.MouseLeave:connect(function()
		RightButton.ImageColor3 = LiftedColor
	end)

	--Backward button pressed/lifted
	DownButton.MouseButton1Down:connect(function()
		Direction = -1
		DownButton.ImageColor3 = PressedColor
	end)
	DownButton.MouseButton1Up:connect(function()
		Direction = 0
		DownButton.ImageColor3 = LiftedColor
	end)
	DownButton.MouseLeave:connect(function()
		DownButton.ImageColor3 = LiftedColor
	end)

	--Forward button pressed/lifted
	UpButton.MouseButton1Down:connect(function()
		Direction = 1
		UpButton.ImageColor3 = PressedColor
	end)
	UpButton.MouseButton1Up:connect(function()
		Direction = 0
		UpButton.ImageColor3 = LiftedColor
	end)
	UpButton.MouseLeave:connect(function()
		UpButton.ImageColor3 = LiftedColor
	end)
end
