Jump to content

Toggle odometer


Absence2

Recommended Posts

Hey there! I'm basically trying to hide the odometer if a player presses 'I' (Opens the inventory) - minus the two errors :lol:

(The odometer stays 'on top' of the inventory while it's shown/opened (> It's under the odometer)

Here's the odometer script:

enginelessVehicle = { [510]=true, [509]=true, [481]=true } 
  
-- draw the odometer as soon as the player enters the car. 
function showOdometer( theVehicle, seat ) 
    if not (guiGetVisible(odometer)) then 
        local id = getElementModel(theVehicle) 
        if (seat < 2) then 
            if not (enginelessVehicle[id]) then -- If the vehicle has an engine. 
                local x, y = guiGetScreenSize() 
                 
                local vehicle = getPedOccupiedVehicle(getLocalPlayer()) 
                local km = getElementData(vehicle, "odometer") 
                 
                local km = math.floor(km/1000) 
                local odoKM = string.format("%07d", km) 
                odometer = guiCreateLabel(x-138, y-215, 200, 200, tostring(odoKM), false) 
                guiSetFont(odometer, "default-bold-small") 
                 
                kilo = guiCreateLabel(x-122, y-202, 200, 200, "km", false) 
                guiSetFont(kilo, "default-bold-small") 
            end  
        end  
    end 
end 
addEventHandler("onClientPlayerVehicleEnter", getLocalPlayer(), showOdometer) 
  
-- hide the odometer as soon as the player exits the car.        
function hideOdometer( ) 
    if not (isVehicleLocked(source)) then 
        setTimer( -- So that the odometer doesn't disappear as soon as you press 'F'. 
        function() 
            if (odometer) then 
                destroyElement(odometer) 
                odometer = nil 
            end 
            if (kilo) then 
                destroyElement(kilo) 
                kilo = nil 
            end 
        end, 1700, 1)    
    end  
end 
addEventHandler("onClientVehicleStartExit", getLocalPlayer(), hideOdometer)  
  
-- update the odometer as the player moves in his vehicle. 
function updateOdometer( currentKilometers ) 
    if (guiGetVisible(odometer)) then 
        local currentKilometers = math.floor(currentKilometers/1000) 
        local odoKM = string.format("%07d", currentKilometers) 
        guiSetText(odometer, tostring(odoKM)) 
    end 
end 
addEvent("updateOdo", true) 
addEventHandler("updateOdo", getLocalPlayer(), updateOdometer) 
  
-- remove the odometer if the player is not a in a vehicle but he is still seeing it. 
function removeOdometer() 
    if not (isPedInVehicle(getLocalPlayer())) then 
        if (odometer) then 
            destroyElement(odometer) 
            odometer = nil 
        end 
        if (kilo) then 
            destroyElement(kilo) 
            kilo = nil 
        end 
    end 
end 
setTimer(removeOdometer, 50, 0) 

And I figure this wont help but I'll post it anyways, it's a function whether to show the inventory window or not.

bindKey( "i", "down", 
    function( ) 
        if (getElementData(localPlayer, "loggedin") == 1) then 
            if show then 
                show = false 
                showCursor( false ) 
                --exports["realism-system"]:showSpeedo() 
            elseif not getElementData(localPlayer, "adminjailed") and not getElementData(localPlayer, "pd.jailstation") then 
                show = true 
                showCursor( true ) 
                --exports["realism-system"]:hideSpeedo() 
            else 
                outputChatBox("You can't access your inventory in jail", 255, 0, 0) 
            end 
        end  
    end 
) 

No idea if existing errors might cause problems, but the errors are:

line 5 & 47: bad argument @ 'guiGetVisible' [Expected gui-element at argument 1, got nil]

line 47 only shows if you're on a bike/bmx/mountain bike etc.

Link to comment

Try

local enginelessVehicle = { [510]=true, [509]=true, [481]=true } 
local odometer 
  
-- draw the odometer as soon as the player enters the car. 
function showOdometer( theVehicle, seat ) 
    if not guiGetVisible( odometer ) then 
        local id = getElementModel( theVehicle ) 
        if (seat < 2) then 
            if not (enginelessVehicle[id]) then -- If the vehicle has an engine. 
                local x, y = guiGetScreenSize() 
                 
                local vehicle = getPedOccupiedVehicle( source ) 
                local km = getElementData(vehicle, "odometer") 
                 
                local km = math.floor(km/1000) 
                local odoKM = string.format("%07d", km) 
                odometer = guiCreateLabel(x-138, y-215, 200, 200, tostring(odoKM), false) 
                guiSetFont(odometer, "default-bold-small") 
                 
                kilo = guiCreateLabel(x-122, y-202, 200, 200, "km", false) 
                guiSetFont(kilo, "default-bold-small") 
            end  
        end  
    end 
end 
addEventHandler("onClientPlayerVehicleEnter", getLocalPlayer(), showOdometer) 
  
-- hide the odometer as soon as the player exits the car.        
function hideOdometer( ) 
    if not (isVehicleLocked(source)) then 
        setTimer( -- So that the odometer doesn't disappear as soon as you press 'F'. 
        function() 
            if (odometer) then 
                destroyElement(odometer) 
                odometer = nil 
            end 
            if (kilo) then 
                destroyElement(kilo) 
                kilo = nil 
            end 
        end, 1700, 1)    
    end  
end 
addEventHandler("onClientVehicleStartExit", getLocalPlayer(), hideOdometer)  
  
-- update the odometer as the player moves in his vehicle. 
function updateOdometer( currentKilometers ) 
    if isElement( odometer ) then 
        if (guiGetVisible(odometer)) then 
            local currentKilometers = math.floor(currentKilometers/1000) 
            local odoKM = string.format("%07d", currentKilometers) 
            guiSetText(odometer, tostring(odoKM)) 
        end 
    end  
end 
addEvent("updateOdo", true) 
addEventHandler("updateOdo", getLocalPlayer(), updateOdometer) 
  
-- remove the odometer if the player is not a in a vehicle but he is still seeing it. 
function removeOdometer() 
    if not (isPedInVehicle(getLocalPlayer())) then 
        if (odometer) then 
            destroyElement(odometer) 
            odometer = nil 
        end 
        if (kilo) then 
            destroyElement(kilo) 
            kilo = nil 
        end 
    end 
end 
setTimer(removeOdometer, 50, 0) 

Updated.

Link to comment

Thanks :D

but line 6 still had error so I added

if isElement( odometer ) then  

like you did:

local enginelessVehicle = { [510]=true, [509]=true, [481]=true } 
local odometer 
  
-- draw the odometer as soon as the player enters the car. 
function showOdometer( theVehicle, seat ) 
    if isElement( odometer ) then 
    if not guiGetVisible( odometer ) then 
        local id = getElementModel( theVehicle ) 
        if (seat < 2) then 
            if not (enginelessVehicle[id]) then -- If the vehicle has an engine. 
                local x, y = guiGetScreenSize() 
                
                local vehicle = getPedOccupiedVehicle( source ) 
                local km = getElementData(vehicle, "odometer") 
                
                local km = math.floor(km/1000) 
                local odoKM = string.format("%07d", km) 
                odometer = guiCreateLabel(x-138, y-215, 200, 200, tostring(odoKM), false) 
                guiSetFont(odometer, "default-bold-small") 
                
                kilo = guiCreateLabel(x-122, y-202, 200, 200, "km", false) 
                guiSetFont(kilo, "default-bold-small") 
            end 
        end 
    end 
end 
end 
addEventHandler("onClientPlayerVehicleEnter", getLocalPlayer(), showOdometer) 
  
-- hide the odometer as soon as the player exits the car.       
function hideOdometer( ) 
    if not (isVehicleLocked(source)) then 
        setTimer( -- So that the odometer doesn't disappear as soon as you press 'F'. 
        function() 
            if (odometer) then 
                destroyElement(odometer) 
                odometer = nil 
            end 
            if (kilo) then 
                destroyElement(kilo) 
                kilo = nil 
            end 
        end, 1700, 1)   
    end 
end 
addEventHandler("onClientVehicleStartExit", getLocalPlayer(), hideOdometer) 
  
-- update the odometer as the player moves in his vehicle. 
function updateOdometer( currentKilometers ) 
    if isElement( odometer ) then 
        if (guiGetVisible(odometer)) then 
            local currentKilometers = math.floor(currentKilometers/1000) 
            local odoKM = string.format("%07d", currentKilometers) 
            guiSetText(odometer, tostring(odoKM)) 
        end 
    end 
end 
addEvent("updateOdo", true) 
addEventHandler("updateOdo", getLocalPlayer(), updateOdometer) 
  
-- remove the odometer if the player is not a in a vehicle but he is still seeing it. 
function removeOdometer() 
    if not (isPedInVehicle(getLocalPlayer())) then 
        if (odometer) then 
            destroyElement(odometer) 
            odometer = nil 
        end 
        if (kilo) then 
            destroyElement(kilo) 
            kilo = nil 
        end 
    end 
end 
setTimer(removeOdometer, 50, 0) 

There is no more errors but how do I hide the odometer when I open the inventory?

Link to comment
local enginelessVehicle = { [510]=true, [509]=true, [481]=true } 
local odometer 
  
-- draw the odometer as soon as the player enters the car. 
function showOdometer( theVehicle, seat ) 
    if not isElement( odometer ) then 
        local id = getElementModel( theVehicle ) 
        if (seat < 2) then 
            if not (enginelessVehicle[id]) then -- If the vehicle has an engine. 
                local x, y = guiGetScreenSize() 
                 
                local vehicle = getPedOccupiedVehicle( source ) 
                local km = getElementData(vehicle, "odometer") 
                 
                local km = math.floor(km/1000) 
                local odoKM = string.format("%07d", km) 
                odometer = guiCreateLabel(x-138, y-215, 200, 200, tostring(odoKM), false) 
                guiSetFont(odometer, "default-bold-small") 
                 
                kilo = guiCreateLabel(x-122, y-202, 200, 200, "km", false) 
                guiSetFont(kilo, "default-bold-small") 
            end  
        end  
    end 
end 
addEventHandler("onClientPlayerVehicleEnter", getLocalPlayer(), showOdometer) 
  
-- hide the odometer as soon as the player exits the car.        
function hideOdometer( ) 
    if not (isVehicleLocked(source)) then 
        setTimer( -- So that the odometer doesn't disappear as soon as you press 'F'. 
        function() 
            if (odometer) then 
                destroyElement(odometer) 
                odometer = nil 
            end 
            if (kilo) then 
                destroyElement(kilo) 
                kilo = nil 
            end 
        end, 1700, 1)    
    end  
end 
addEventHandler("onClientVehicleStartExit", getLocalPlayer(), hideOdometer)  
  
-- update the odometer as the player moves in his vehicle. 
function updateOdometer( currentKilometers ) 
    if isElement( odometer ) then 
        if (guiGetVisible(odometer)) then 
            local currentKilometers = math.floor(currentKilometers/1000) 
            local odoKM = string.format("%07d", currentKilometers) 
            guiSetText(odometer, tostring(odoKM)) 
        end 
    end  
end 
addEvent("updateOdo", true) 
addEventHandler("updateOdo", getLocalPlayer(), updateOdometer) 
  
-- remove the odometer if the player is not a in a vehicle but he is still seeing it. 
function removeOdometer() 
    if not (isPedInVehicle(getLocalPlayer())) then 
        if (odometer) then 
            destroyElement(odometer) 
            odometer = nil 
        end 
        if (kilo) then 
            destroyElement(kilo) 
            kilo = nil 
        end 
    end 
end 
setTimer(removeOdometer, 50, 0) 

There is no more errors but how do I hide the odometer when I open the inventory?

Not understand

Link to comment
local enginelessVehicle = { [510]=true, [509]=true, [481]=true } 
local odometer 
  
-- draw the odometer as soon as the player enters the car. 
function showOdometer( theVehicle, seat ) 
    if not isElement( odometer ) then 
        local id = getElementModel( theVehicle ) 
        if (seat < 2) then 
            if not (enginelessVehicle[id]) then -- If the vehicle has an engine. 
                local x, y = guiGetScreenSize() 
                 
                local vehicle = getPedOccupiedVehicle( source ) 
                local km = getElementData(vehicle, "odometer") 
                 
                local km = math.floor(km/1000) 
                local odoKM = string.format("%07d", km) 
                odometer = guiCreateLabel(x-138, y-215, 200, 200, tostring(odoKM), false) 
                guiSetFont(odometer, "default-bold-small") 
                 
                kilo = guiCreateLabel(x-122, y-202, 200, 200, "km", false) 
                guiSetFont(kilo, "default-bold-small") 
            end  
        end  
    end 
end 
addEventHandler("onClientPlayerVehicleEnter", getLocalPlayer(), showOdometer) 
  
-- hide the odometer as soon as the player exits the car.        
function hideOdometer( ) 
    if not (isVehicleLocked(source)) then 
        setTimer( -- So that the odometer doesn't disappear as soon as you press 'F'. 
        function() 
            if (odometer) then 
                destroyElement(odometer) 
                odometer = nil 
            end 
            if (kilo) then 
                destroyElement(kilo) 
                kilo = nil 
            end 
        end, 1700, 1)    
    end  
end 
addEventHandler("onClientVehicleStartExit", getLocalPlayer(), hideOdometer)  
  
-- update the odometer as the player moves in his vehicle. 
function updateOdometer( currentKilometers ) 
    if isElement( odometer ) then 
        if (guiGetVisible(odometer)) then 
            local currentKilometers = math.floor(currentKilometers/1000) 
            local odoKM = string.format("%07d", currentKilometers) 
            guiSetText(odometer, tostring(odoKM)) 
        end 
    end  
end 
addEvent("updateOdo", true) 
addEventHandler("updateOdo", getLocalPlayer(), updateOdometer) 
  
-- remove the odometer if the player is not a in a vehicle but he is still seeing it. 
function removeOdometer() 
    if not (isPedInVehicle(getLocalPlayer())) then 
        if (odometer) then 
            destroyElement(odometer) 
            odometer = nil 
        end 
        if (kilo) then 
            destroyElement(kilo) 
            kilo = nil 
        end 
    end 
end 
setTimer(removeOdometer, 50, 0) 

There is no more errors buthow do I hide the odometer when I open the inventory?

Not understand

I haven't found any functions yet but I'll keep looking BUT > found a bug after adding:

    if isElement( odometer ) then 

The current kilometers disappears :S

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...