Jump to content

Mechanic job [HELP]


Hell-Mate

Recommended Posts

There were quite many mistakes in the code. You had some unnecessary repeated if statements and some variables that weren't necessarily have to be initiated (try to keep the amount of variables low, for memory reasons really). Also, don't use the local player as an event source, as it won't work like that.

I haven't tested the code as I am not able to do that right now, but let me know of any errors in debug unless you figure them out yourself, in fact, I hope you'll at least try to do that.

Server-side

local pending_vehicles = { } 
local skinmechanic = { 
    [ 50 ] = true 
} 
  
addEvent( getResourceName( resource ) .. ":accepted", true )      
addEventHandler( getResourceName( resource ) .. ":accepted", root, 
    function( ) 
        local vehicle = getPedOccupiedVehicle( client ) 
        if ( not vehicle ) or ( getPedOccupiedVehicleSeat( client ) ~= 1 ) or ( not skinmechanic[ getElementModel( client ) ] ) then return end 
        local driver = getVehicleController( vehicle ) 
        if ( driver ) and ( getElementType( driver ) == "player" ) then 
            if ( getElementHealth( vehicle ) < 800 ) then 
                fadeCamera( source, false, 1.0 ) 
                setTimer( function( vehicle, source ) 
                    if ( isElement( vehicle ) ) and ( getElementHealth( vehicle ) < 800 ) then 
                        givePlayerMoney( source, 3000 ) 
                        fixVehicle( vehicle ) 
                    end 
                    if ( pending_vehicles[ source ] ) then 
                        pending_vehicles[ source ] = nil 
                    end 
                    fadeCamera( source, true, 1.0 ) 
                end, 2500, 1, vehicle, source ) 
            else          
                outputChatBox( "Error: The vehicle is already in good condition.", source, 255, 0, 0, false ) 
            end 
        end 
    end 
) 
  
local vehicle_types = { [ "Automobile" ] = true, [ "Helicopter" ] = true, [ "Monster Truck" ] = true } 
addEventHandler( "onPlayerVehicleEnter", root, 
    function( vehicle, seat, jacked ) 
        if ( jacked ) or ( not skinmechanic[ getElementModel( source ) ] ) or ( not vehicle_types[ getVehicleType( vehicle ) ] ) or ( seat ~= 1 ) then return end 
        local driver = getVehicleController( vehicle ) 
        if ( driver ) and ( getElementType( driver ) == "player" ) then 
            playSoundFrontEnd( source, 40 ) 
            pending_vehicles[ source ] = driver 
            triggerClientEvent( driver, getResourceName( resource ) .. ":display", source ) 
        end  
    end 
) 
  
addEventHandler( "onPlayerVehicleStartExit", root, 
    function( vehicle ) 
        if ( not skinmechanic[ getElementModel( source ) ] ) or ( not vehicle_types[ getVehicleType( vehicle ) ] ) then return end 
        if ( pending_vehicles[ source ] ) and ( isElement( pending_vehicles[ source ] ) ) then 
            triggerClientEvent( pending_vehicles[ source ], getResourceName( resource ) .. ":display", source, false ) 
            pending_vehicles[ source ] = nil 
        end 
    end 
) 

 

Client-side

local theMechanic 
local GUIEditor = { 
    button = { }, 
    window = { }, 
    label = { } 
} 
  
function toggleMechanicDisplay( forced_state ) 
    if ( ( not forced_state ) and ( isElement( GUIEditor.window[ 1 ] ) ) ) or ( isElement( GUIEditor.window[ 1 ] ) ) then 
        destroyElement( GUIEditor.window[ 1 ] ) 
        showCursor( false ) 
        return 
    end 
     
    GUIEditor.window[ 1 ] = guiCreateWindow( 526, 287, 334, 142, "Mechanic", false ) 
    guiWindowSetSizable( GUIEditor.window[ 1 ], false ) 
     
    GUIEditor.label[ 1 ] = guiCreateLabel( 60, 42, 209, 17, "Do you accept to get your car repaired?", false, GUIEditor.window[ 1 ] ) 
    GUIEditor.button[ 1 ] = guiCreateButton( 9, 92, 150, 40, "Accept", false, GUIEditor.window[ 1 ] ) 
    GUIEditor.button[ 2 ] = guiCreateButton( 169, 91, 155, 41, "Close", false, GUIEditor.window[ 1 ] ) 
     
    addEventHandler( "onClientGUIClick", GUIEditor.button[ 1 ], 
        function( ) 
            if ( isElement( theMechanic ) ) then 
                triggerServerEvent( getResourceName( resource ) .. ":accepted", theMechanic ) 
            end 
            toggleMechanicDisplay( ) 
        end 
    ) 
     
    addEventHandler( "onClientGUIClick", GUIEditor.button[ 2 ], 
        function( ) 
            toggleMechanicDisplay( ) 
        end 
    ) 
     
    showCursor( true ) 
end 
  
addEvent( getResourceName( resource ) .. ":display", true ) 
addEventHandler( getResourceName( resource ) .. ":display", root, 
    function( forced_state ) 
        if ( isElement( source ) ) and ( getElementType( source ) == "player" ) then 
            theMechanic = source 
            toggleMechanicDisplay( forced_state or true ) 
        else 
            outputDebugString( "An alien tried to abduct you...", 2 ) 
        end 
    end 
) 

Edited by Guest
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...