local function FR_DrivingAnimiation(player)
	local vehicle = player:getVehicle()
	if not vehicle then return end
	local vehicleName = vehicle:getScriptName()
	--DebugLog.log("FR_DrivingAnimiation: The player, " .. tostring(player:getUsername()) .. ", entered " .. tostring(vehicleName))
    if not vehicleName:contains("fr_") then return end
	
	local passCompPart = vehicle:getPartById("PassengerCompartment")
	local keyvalues = passCompPart:getTable("FRSeatAnimations")
	local seatPos = vehicle:getSeat(player)
	--DebugLog.log ("FR_DrivingAnimiation: seatPos is " .. seatPos)
	local seatAnim = ""
	if keyvalues and passCompPart then
--SeatFrontLeft starts at 0
		if seatPos == 0 then
		seatAnim = keyvalues.seat0
		elseif seatPos == 1 then
		seatAnim = keyvalues.seat1
		elseif seatPos == 2 then
		seatAnim = keyvalues.seat2
		elseif seatPos == 3 then
		seatAnim = keyvalues.seat3
		elseif seatPos == 4 then
		seatAnim = keyvalues.seat4
		elseif seatPos == 5 then
		seatAnim = keyvalues.seat5
		elseif seatPos == 6 then
		seatAnim = keyvalues.seat6
		elseif seatPos == 7 then
		seatAnim = keyvalues.seat7
		elseif seatPos == 8 then
		seatAnim = keyvalues.seat8
		elseif seatPos == 9 then
		seatAnim = keyvalues.seat9
		elseif seatPos == 10 then
		seatAnim = keyvalues.seat10
		elseif seatPos == 11 then
		seatAnim = keyvalues.seat11
		end
		seatAnim = tostring(seatAnim or keyvalues.seatAll or "")
		--DebugLog.log ("FR_DrivingAnimiation: seatAnim is " .. tostring(seatAnim))
		--This sends the info to the server so it can tell the other players to sync the animations
		--DebugLog.log("FR_DrivingAnimiation: isClient: " .. tostring(isClient()) .. ", isServer: " .. tostring(isServer()))
		if isClient() then
			local args = { seatAnim = seatAnim, onlineID = player:getOnlineID() }
			--DebugLog.log("FR_DrivingAnimiation: isClient: " .. tostring(isClient()) .. ", should be multiplayer. Sending info to be set later. isServer: " .. tostring(isServer()))
			--DebugLog.log("FR_DrivingAnimiation: player is " .. tostring(player) .. ", username is " .. tostring(player:getUsername()) .. ", onlineID is " .. tostring(player:getOnlineID()))
			sendClientCommand(player, 'FR_VehicleAnimations', 'FR_SendPlayerAnimation', args)
		else
			--DebugLog.log("FR_DrivingAnimiation: isClient: " .. tostring(isClient()) .. ", should be singleplayer, setting variable directly.")
			player:SetVariable("FR_Animation", seatAnim)
		end
	end	
end

local function FR_ExitAnimation(player)
	local seatAnim = ""
	--DebugLog.log("FR_ExitAnimation: isClient: " .. tostring(isClient()) .. ", isServer: " .. tostring(isServer()))
	--DebugLog.log("FR_DrivingAnimiation: The player, " .. tostring(player:getUsername()) .. ", exited a vehicle")
	if isClient() then
		local args = { seatAnim = seatAnim, onlineID = player:getOnlineID() }
		sendClientCommand(player, 'FR_VehicleAnimations', 'FR_SendPlayerAnimation', args)
		--DebugLog.log("FR_ExitAnimation: isClient: " .. tostring(isClient()) .. ", should be multiplayer. , isServer: " .. tostring(isServer()))
		--DebugLog.log("FR_ExitAnimation: player is " .. tostring(player) .. ", username is " .. tostring(player:getUsername()) .. ", online Id is " .. tostring(player:getOnlineID()))
		--DebugLog.log("FR_ExitAnimation: player is " .. tostring(getPlayerByOnlineID(player:getOnlineID())))
	else
		player:SetVariable("FR_Animation", seatAnim)
		--DebugLog.log("FR_ExitAnimation: isClient: " .. tostring(isClient()) .. ", should be singleplayer, setting variable directly.")
	end
end

Events.OnEnterVehicle.Add(FR_DrivingAnimiation)
Events.OnSwitchVehicleSeat.Add(FR_DrivingAnimiation)
Events.OnExitVehicle.Add(FR_ExitAnimation)