--Original script by Luckymaxer



Model = script.Parent
local SoundLoop
local Whinny
sound = false
Players = game:GetService("Players")
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")

Humanoid = Model:WaitForChild("Humanoid")
Humanoid.Health = Humanoid.MaxHealth
Seat = Model:WaitForChild("VehicleSeat")
Torso = Model:WaitForChild("Torso")

InteractiveControl = script:WaitForChild("InteractiveControl")

Animations = {
	Walk = {Animation = script:WaitForChild("Walk"), FadeTime = nil, Weight = nil, Speed = 7},
	Idle = {Animation = script:WaitForChild("Idle"), FadeTime = nil, Weight = nil, Speed = 1},
	Ride = {Animation = script:WaitForChild("Ride"), FadeTime = 0.25, Weight = nil, Speed = nil},
	Rear = {Animation = script:WaitForChild("Rearing"), FadeTime = 0.25, Weight = nil, Speed = 2},
	Jump = {Animation = script:WaitForChild("Jump"), FadeTime = nil, Weight = nil, Speed = 4},
}

Walking = false
Rider = nil

AngularVelocity = (Torso:FindFirstChild("BodyAngularVelocity") or Instance.new("BodyAngularVelocity"))
AngularVelocity.Name = "BodyAngularVelocity"
AngularVelocity.P = 1250
AngularVelocity.AngularVelocity = Vector3.new(0, 0, 0)
AngularVelocity.maxTorque = Vector3.new(0, math.huge, 0)
AngularVelocity.Parent = Torso

Velocity = (Torso:FindFirstChild("BodyVelocity") or Instance.new("BodyVelocity"))
Velocity.Name = "BodyVelocity"
Velocity.maxForce = Vector3.new(math.huge, 0, math.huge)
Velocity.Parent = Torso

WalkTrack = Humanoid:LoadAnimation(Animations.Walk.Animation)
IdleTrack = Humanoid:LoadAnimation(Animations.Idle.Animation)
RearTrack = Humanoid:LoadAnimation(Animations.Rear.Animation)
JumpTrack = Humanoid:LoadAnimation(Animations.Jump.Animation)

Remotes = script:WaitForChild("Remotes")
Remotes:ClearAllChildren()

LastJump = 0
Disabled = false

ServerControl = (Remotes:FindFirstChild("ServerControl") or Instance.new("RemoteFunction"))
ServerControl.Name = "ServerControl"
ServerControl.Parent = Remotes

ClientControl = (Remotes:FindFirstChild("ClientControl") or Instance.new("RemoteFunction"))
ClientControl.Name = "ClientControl"
ClientControl.Parent = Remotes

function OnServerInvoke(player, Mode, Value)
	if player ~= Rider or not Mode then
		return
	end
	if Mode == "Jump" then
		local Now = tick()
		if (Now - LastJump) >= 1 then
			LastJump = Now
			Humanoid.Jump = true
				local Animation = Animations.Jump
				JumpTrack:Play(Animation.FadeTime, Animation.Weight, Animation.Speed)
		end
	elseif Mode == "Dismount" then
		Dismount()
	end
end

ServerControl.OnServerInvoke = OnServerInvoke

Humanoid.Changed:connect(function(Property)
	if Property == "Sit" and Humanoid.Sit then
		Humanoid.Sit = false
	end
end)

Humanoid.Died:connect(function()
	Disabled = true
	Debris:AddItem(Model, 3)
	Model:BreakJoints()
end)

function InvokeClient(Mode, Value)
	local ClientReturn = nil
	pcall(function()
		ClientReturn = ClientControl:InvokeClient(Rider, Mode, Value)
	end)
	return ClientReturn
end

function Audio(ID, Location, Looped, Volume, Duration)
	local Sound = Instance.new("Sound", Location)
	Sound.SoundId = "http://www.roblox.com/asset/?id=" .. ID
	Sound.Volume = Volume
	Sound.Looped = Looped
	if Duration then
		game:GetService("Debris"):AddItem(Sound, Duration)
	end
	return Sound -- use as Audio(134973578, FrontTorso, true, 2)
end

function Animate(Time)
	if Humanoid.Health == 0 or Disabled then
		return
	end
	--Walking
	if not Walking and Torso.Velocity.magnitude > 1 then
		IdleTrack:Stop()
		local Animation = Animations.Walk
		WalkTrack:Play(Animation.FadeTime, Animation.Weight, Animation.Speed)
		Walking = true
	end
	if Walking and Torso.Velocity.magnitude < 1 then
		WalkTrack:Stop()
		local Animation = Animations.Idle
		IdleTrack:Play(Animation.FadeTime, Animation.Weight, Animation.Speed)
		Walking = false
	end
	--Motion
	if Seat.Throttle ~= -1 then
	Velocity.velocity = (Torso.CFrame.lookVector * Seat.Throttle * Humanoid.WalkSpeed*-1)
	AngularVelocity.AngularVelocity = Vector3.new(0, (2 * -Seat.Steer), 0)--steer speed
	else
	Velocity.velocity = Vector3.new(0,0, 0) --Erase for moonwalk
	Whinny = Audio(178645076, Torso, false, 3)
		if sound==false then
			sound = true
				local Animation = Animations.Rear
				RearTrack:Play(Animation.FadeTime, Animation.Weight, Animation.Speed)
				Whinny:Play()
				Whinny.Volume = 10
				Whinny.Pitch = 1
				wait(2)
			sound = false
		end
	end
		
	--Sound	
	if not SoundLoop then
		SoundLoop = Audio(134973578, Torso, true, 0)
	elseif Torso.Velocity.magnitude > 1 then
		if not SoundLoop.IsPlaying then
			SoundLoop:Play()
		end
		SoundLoop.Volume = 10
		SoundLoop.Pitch = ((1.2 - .55) * (Torso.Velocity.Magnitude / script.Parent.MaxSpeed.Value)) + .55
	else
		SoundLoop:Stop()
	end
	if JumpTrack.IsPlaying then
		SoundLoop:Stop()
	end
end

function Dismount()
	if Rider and Rider.Parent then
		local Character = Rider.Character
		if Character then
			local Humanoid = nil
			for i, v in pairs(Character:GetChildren()) do
				if v:IsA("Humanoid") then
					Humanoid = v
					break
				end
			end
			Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			Humanoid.Jump = true
		end
	end
end

function PlayerSeated(Child)
	local function BreakWeld()
		Debris:AddItem(Child, 0.5)
		if Child and Child.Parent then
			Child:Destroy()
		end
	end
	if not Child:IsA("Weld") or not Child.Name == "SeatWeld" then
		return
	end
	if Child.Part0 ~= Seat or not Child.Part1 or not Child.Part1.Parent then
		return
	end
	Child.C1 = (Child.C1 * CFrame.new(0, 0.5, 0.375))
	local character = Child.Part1.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
		local Animation = Animations.Ride
		RideTrack = humanoid:LoadAnimation(Animation.Animation)
		RideTrack:Play(Animation.FadeTime, Animation.Weight, Animation.Speed)
	end
	local Player = Players:GetPlayerFromCharacter(character)
	if not Player then
		BreakWeld()
		return
	end
	local PlayerGui = Player:FindFirstChild("PlayerGui")
	if not PlayerGui then
		BreakWeld()
		return
	end
	InteractiveControlCopy = InteractiveControl:Clone()
	local ServerCommunication = Instance.new("ObjectValue")
	ServerCommunication.Name = "ServerControl"
	ServerCommunication.Value = ServerControl
	ServerCommunication.Parent = InteractiveControlCopy
	InteractiveControlCopy.Disabled = false
	InteractiveControlCopy.Parent = PlayerGui
	Rider = Player
end

function PlayerDismounted(Child)
	if not Child:IsA("Weld") or not Child.Name == "SeatWeld" then
		return
	end
	if not Child.Part1 or not Child.Part1.Parent then
		return
	end
	local Player = Players:GetPlayerFromCharacter(Child.Part1.Parent)
	if not Player or Player ~= Rider then
		return
	end
	if InteractiveControlCopy and InteractiveControlCopy.Parent then
		InteractiveControlCopy:Destroy()
	end
	if RideTrack then
		RideTrack:Stop()
	end
	Rider = nil
end

RunService.Heartbeat:connect(Animate)
Seat.ChildAdded:connect(PlayerSeated)
Seat.ChildRemoved:connect(PlayerDismounted)