-- Helper function

function SetInformation(index)
	bg.T.Text = Vehicles[index].Name
end

local locked = false
bg.S.MouseButton1Click:connect(function()
	if (locked) then return end
	locked = true
	local vehicleName = player.Name.."'s Car"
	if game.Workspace:FindFirstChild(player.Name.."'s Car") then
		game.Workspace[player.Name.."'s Car"]:Destroy()
	end
	if (not workspace:FindFirstChild(vehicleName)) then
		local model = Vehicles[index]:Clone()
		model.Name = vehicleName
		model.Parent = game.Workspace
		model:MakeJoints()
		model:MoveTo(player.Character.Torso.Position + Vector3.new(0, 0, 0))
		bg.Visible = false
	end	
	locked = false
end)

bg.B.MouseButton1Click:connect(function()
	index = index - 1
	if (index < 1) then index = #Vehicles end
	SetInformation(index)
end)

bg.N.MouseButton1Click:connect(function()
	index = index + 1
	if (index > #Vehicles) then index = 1 end
	SetInformation(index)
end)

SetInformation(index)
