local B=false local a=false local R,N,o,l,L,w=Vector3.new,CFrame.new,math.rad,math.pi,CFrame.Angles,Ray.new local function P(B,a)a(B)for B,R in next,B:GetChildren()do P(R,a)end end function Hopoff()if SegwayObject.Value~=nil and(B==false and(SegwayObject.Value.PrimaryPart and SeaterObject.Value~=nil))then B=true a=false local o=SegwayObject.Value.PrimaryPart local l,L,w,P,O,D,Y,x,z,s,G,C=o.CFrame:components()ContextService:UnbindAction("\115\101\103\073\110\112\117\116\065\099\116\105\111\110")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=R(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(N(l,L+.1,w,1,O,D,Y,1,z,s,G,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(o,true)UndoTags:FireServer(SegwayObject,WeldObject,TiltMotor)ConfigHumanoid:FireServer(Humanoid,false,true,true)wait(1)AnchorSegway:FireServer(o,false)UndoHasWelded:FireServer(SeaterObject)Direction=0 Steer=0 B=false end end function UpdateCharTrans(B,a)P(Character,function(R)if R and R:IsA("\066\097\115\101\080\097\114\116")then if R.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")or R.Parent:IsA("\065\099\099\101\115\115\111\114\121")then R.LocalTransparencyModifier=B else R.LocalTransparencyModifier=a end elseif R and(R:IsA("\068\101\099\097\108")and R.Name=="\102\097\099\101")then R.Transparency=B end end)end function UpdateVRPos()if UserInputService.VREnabled and(Character and(Character.Parent and Character:FindFirstChild("\072\101\097\100")))then local B,a,o,l,L,w,P,O,D,Y,x,z=Character.Head.CFrame:components()Camera.CFrame=N(B,a,o,l,0,w,0,1,0,Y,0,z)+R(L,O,x)*2.5 end end function Accelerate()if a then local B=(Direction*Thruster.Value.CFrame.lookVector)*MaxSpeed.Value local a=Thruster.Value.Velocity:Lerp(B,.05)local N=R(0,(-Steer*TurnSpeed.Value)/10,0)local o,l,L,w,P,O,D,Y,x,z,s,G=Thruster.Value.CFrame:components()if Y>.7 then Thruster.Value.BodyGyro.MaxTorque=R(400000,0,400000)Thruster.Value.Velocity=a Thruster.Value.RotVelocity=N else Thruster.Value.BodyGyro.MaxTorque=R(0,0,0)end end end function CastToGround()if Thruster.Value and Thruster.Value.Parent then local B,a,o,P,O,D,Y,x,z,s,G,C=Thruster.Value.CFrame:components()local c=-R(O,x,G)local J=w(Thruster.Value.CFrame.p,c*50)local v,M,W=workspace:FindPartOnRayWithIgnoreList(J,{SegwayObject.Value;RayTest})local j=(Thruster.Value.Position-M).magnitude local d=(W-c).magnitude if v and(v.CanCollide and d>1.7)then Thruster.Value.BodyGyro.MaxTorque=R(400000,0,400000)Thruster.Value.BodyGyro.CFrame=N(M,M+W)*L(-l/2,0,0)end end end function Sound()if Thruster.Value and.85+o(Thruster.Value.Velocity.Magnitude/1.8)<=RunSoundPitchLimit then SeaterObject.Value.Parent.Run.Pitch=.85+o(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+o(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 function onGENInput(B,a,R)if WeldObject.Value~=nil then local B=R.KeyCode if(B==Enum.KeyCode.Space or B==Enum.KeyCode.Backspace or B==Enum.KeyCode.ButtonA)and a==Enum.UserInputState.Begin then Hopoff()end if B==Enum.KeyCode.W or B==Enum.KeyCode.Up or B==Enum.KeyCode.ButtonR2 then Direction=a==Enum.UserInputState.Begin and 1 or Direction==1 and 0 or Direction end if B==Enum.KeyCode.S or B==Enum.KeyCode.Down or B==Enum.KeyCode.ButtonL2 then Direction=a==Enum.UserInputState.Begin and-1 or Direction==-1 and 0 or Direction end if B==Enum.KeyCode.D or B==Enum.KeyCode.Right then Steer=a==Enum.UserInputState.Begin and 1 or Steer==1 and 0 or Steer end if B==Enum.KeyCode.A or B==Enum.KeyCode.Left then Steer=a==Enum.UserInputState.Begin and-1 or Steer==-1 and 0 or Steer end end end

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()
	
	ContextService:UnbindAction("segInputAction")
		
	-- 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

function onGENInput(actionName,inputState,inputObj)
	if WeldObject.Value ~= nil then
		local Key=inputObj.KeyCode
		
		-- Hop off segway if press Spacebar, Backspace or controller A button
		if (Key == Enum.KeyCode.Space or Key == Enum.KeyCode.Backspace or Key == Enum.KeyCode.ButtonA) and inputState == Enum.UserInputState.Begin then	
			Hopoff()
		end
		
		--Forward
		if Key == Enum.KeyCode.W or Key == Enum.KeyCode.Up or Key == Enum.KeyCode.ButtonR2 then
			Direction = (inputState == Enum.UserInputState.Begin and 1) or (Direction == 1 and 0) or Direction
		end
		
		--Backward
		if Key == Enum.KeyCode.S or Key == Enum.KeyCode.Down or Key == Enum.KeyCode.ButtonL2 then
			Direction = (inputState == Enum.UserInputState.Begin and -1) or (Direction == -1 and 0) or Direction
		end
		
		--Left
		if Key == Enum.KeyCode.D or Key == Enum.KeyCode.Right then
			Steer = (inputState == Enum.UserInputState.Begin and 1) or (Steer == 1 and 0) or Steer
		end
		
		--Right
		if Key == Enum.KeyCode.A or Key == Enum.KeyCode.Left then
			Steer = (inputState == Enum.UserInputState.Begin and -1) or (Steer == -1 and 0) or Steer
		end
	end
end
