Jump to content

Chose between 2 things


dragonofdark

Recommended Posts

Hello,

I made a little GUI for my bus system, which let us chose which route to take (city or countryside). Normally I have just one route so I have this :

routes = 
{ 
    { 
        { x=1802.39, y=-2072.26, z=13.64, hint = "City Bus Line, just follow the checkpoints." }, 
        { x=1964.35, y=-1959.11, z=13.79 }, 
        { x=1784.24, y=-1826.28, z=13.51, hint = "Bus Stop 1 : Unity Station", stop = true }, 
        -- and all my checkpoints/stops for this route 
    } 
} 

and in my script I have that :

  
local function newRoute( player ) 
    p[ player ].vehicleOnResourceStart = nil 
    local route = math.random( #routes ) 
    p[ player ].route = routes[ route ] 
    p[ player ].checkpoint = 0 
     
    advanceRoute( player ) 
end 

advanceRoute is the function which unPack all coordinates,..

So, How can I modify this to make 2 routes (first code) and if I push "this" button then it loads "this" route ?

Thanks

Link to comment
routes = {} 
routes["city"] = 
{ 
    { 
        { x=1802.39, y=-2072.26, z=13.64, hint = "City Bus Line, just follow the checkpoints." }, 
        { x=1964.35, y=-1959.11, z=13.79 }, 
        { x=1784.24, y=-1826.28, z=13.51, hint = "Bus Stop 1 : Unity Station", stop = true }, 
        -- and all my checkpoints/stops for this route 
    } 
} 
  
routes["country"] = 
{ 
    { 
        { x=1802.39, y=-2072.26, z=13.64, hint = "Country Bus Line, just follow the checkpoints." }, 
        { x=1964.35, y=-1959.11, z=13.79 }, 
        { x=1784.24, y=-1826.28, z=13.51, hint = "Bus Stop 1 : Unity Station", stop = true }, 
        -- and all my checkpoints/stops for this route 
    } 
} 

I would use element data to set the routes, then you can do this:

local function newRoute( player ) 
    p[ player ].vehicleOnResourceStart = nil 
    local route = math.random( #routes[getElementData(player,"route")] ) 
    p[ player ].route = routes[getElementData(player,"route")][ route ] 
    p[ player ].checkpoint = 0 
    
    advanceRoute( player ) 
end 

But, I think you'll need to modify advanceRoute function also.

Try with that (Not tested).

Link to comment

if you got again this error

attempt to get length of field '?' (a nil value) 

then the getElementData(....,"route") its nil (no data is stored or is sotred wrong)

and for button's you wanna 2 butons if i pres the city then stores the city variable and ....

use

  
local citybutton  = guiCreateButton(.......) 
"onClientGUIClick",citybutton,and the function name from city ariables 
  

Link to comment

dragon, that's because his reply was the same code as mine, but added "1" which is not required.

Now, did you set the element data to the player? if you didnt, then it'll never work.

function onGUIClick() 
if (source == cityButton) then 
setElementData(localPlayer,"route","city") 
elseif (source == countryButton) then 
setElementData(localPlayer,"route","country") 
     end 
end 
addEventHandler("onClientGUIClick",root,onGUIClick) 

Of course, you need the two buttons.

Link to comment

route.lua (the list of checkpoints)

routes = {} 
routes["city"] = 
{ 
    { 
        { x=1802.39, y=-2072.26, z=13.64, hint = "C'est tres simple, suivez la route jusqu'au premier arret. Pour tous les points jaunes, il est suffisant de passer dedans." }, 
        { x=1964.35, y=-1959.11, z=13.79 }, 
        { x=1824.07, y=-1858.88, z=13.49, hint = "Ralentissez ! Il y a un arret devant et vous devez attendre quelques secondes avant de continuer votre route." }, 
        { x=1784.24, y=-1826.28, z=13.51, hint = "Arret actuel : Gare centrale.\nProchain arret : Pershing Square.", stop = true }, 
        { x=1692.29, y=-1750.9, z=13.48 }, 
        { x=1496.73, y=-1728.63, z=13.51, hint = "Arret actuel : Pershing Square.\nProchain arret : St-Lawrence Blvd.", stop = true }, 
        { x=1386.77, y=-1806.54, z=13.48 }, 
        { x=1316.22, y=-1828.36, z=13.55, hint = "Arret actuel : St-Lawrence Blvd.\nProchain arret : Hopital.", stop = true }, 
        { x=1354.21, y=-1428.68, z=13.48 }, 
        { x=1177.48, y=-1391.7, z=13.49, hint = "Arret actuel : Hopital.\nProchain arret : Librairie.", stop = true }, 
        { x=914.78, y=-1514.97, z=13.6 }, 
        { x=821.54, y=-1625.65, z=13.51, hint = "Arret actuel : Librairie.\nProchain arret : Port de plaisance.", stop = true }, 
        { x=782.39, y=-1771.37, z=13.34 }, 
        { x=455.41, y=-1757.26, z=5.74 }, 
        { x=165.62, y=-1732.64, z=4.69, hint = "Arret actuel : Port de plaisance.\nProchain arret : Banque ING.", stop = true }, 
        { x=296.53, y=-1415.17, z=14.07 }, 
        { x=564.04, y=-1245.1, z=17.28, hint = "Arret actuel : Banque ING.\nProchain arret : Cimetiere.", stop = true }, 
        { x=794.35, y=-1091.09, z=24.18 }, 
        { x=966.31, y=-1113.29, z=23.79, hint = "Arret actuel : Cimetiere.\nProchain arret : W. Broadway.", stop = true }, 
        { x=1058.31, y=-1041.8, z=32.17 }, 
        { x=1122.6, y=-959.4, z=42.76, hint = "Arret actuel : W. Broadway.\nProchain arret : Banque BNP-Paribas.", stop = true }, 
        { x=1369, y=-1037.64, z=26.339 }, 
        { x=1459.65, y=-1037.55, z=23.79, hint = "Arret actuel : Banque BNP-Paribas.\nProchain arret : St-Lawrence Blvd.", stop = true }, 
        { x=1452.9, y=-1218.06, z=15.57 }, 
        { x=1339.18, y=-1313.67, z=13.54, hint = "Arret actuel : St-Lawrence Blvd.\nProchain arret : St-Lawrence Blvd.", stop = true }, 
        { x=1419.39, y=-1443.15, z=13.52 }, 
        { x=1490.71, y=-1595.67, z=13.51, hint = "Arret actuel : St-Lawrence Blvd.\nProchain arret : Prison.", stop = true }, 
        { x=1825.08, y=-1593.21, z=13.49, hint = "Arret actuel : Prison.\nProchain arret : Glenpark.", stop = true }, 
        { x=1852.7, y=-1291.64, z=13.51 }, 
        { x=2000.21, y=-1264.62, z=23.9, hint = "Arret actuel : Glenpark.\nProchain arret : Coutt and Schutz.", stop = true }, 
        { x=2153.27, y=-1120.19, z=25.4, hint = "Arret actuel : Coutt and Schutz.\nProchain arret : Club de StripTease.", stop = true }, 
        { x=2367.98, y=-1162.99, z=27.56 }, 
        { x=2425.04, y=-1260.31, z=23.96, hint = "Arret actuel : Club de StripTease.\nProchain arret : Saints Blvd (haut).", stop = true }, 
        { x=2477.72, y=-1186.71, z=39.44 }, 
        { x=2705.904, y=-1187.57, z=69.37, hint = "Arret actuel : Saints Blvd (haut).\nProchain arret : Saints Blvd.", stop = true }, 
        { x=2718.82, y=-1482.23, z=30.41, hint = "Arret actuel : Saints Blvd.\nProchain arret : Plage Est.", stop = true }, 
        { x=2865.68, y=-1514.6, z=11.01 }, 
        { x=2833.7, y=-1779.46, z=11 }, 
        { x=2854.3, y=-1958.55, z=11.07, hint = "Arret actuel : Plage Est.\nProchain arret : Ten Green Bottles.", stop = true }, 
        { x=2835.55, y=-1918.01, z=11.07 }, 
        { x=2532.75, y=-1729.7, z=13.51 }, 
        { x=2316.57, y=-1655.24, z=14.18, hint = "Arret actuel : Ten Green Bottles.\nProchain arret : Station service TOTAL.", stop = true }, 
        { x=2180.45, y=-1746.95, z=13.5 }, 
        { x=1920.82, y=-1748.64, z=13.52, hint = "Arret actuel : Station service TOTAL.\nProchain arret : Gare centrale.", stop = true }, 
        { x=1789.22, y=-1851.71, z=13.54, hint = "Arret actuel : Gare centrale.\nPour terminer votre service, allez garer ce bus au dépôt.", stop = true }, 
        { x=1807.93, y=-2072.05, z=13.55 }, 
    } 
} 
  
routes["country"] = 
{ 
    { 
        { x=1802.39, y=-2072.26, z=13.64, hint = "Country Bus Line, just follow the checkpoints." }, 
        { x=1964.35, y=-1959.11, z=13.79 }, 
        { x=1784.24, y=-1826.28, z=13.51, hint = "Bus Stop 1 : Unity Station", stop = true }, 
        -- I need to finish these checkpoints 
    } 
} 

bus.lua

local vehicles = get( "vehicles" ) or { "Coach" } -- Charge les véhicules qui commence le job après être entrer dedans 
local max_earnings = tonumber( get( "earnings" ) ) or 10 
local delay = tonumber( get( "delay" ) ) or 5 
  
-- put it in a for us better format 
local vehicles2 = { } 
for key, value in ipairs( vehicles ) do 
    local model = getVehicleModelFromName( value ) 
    if model then 
        vehicles2[ model ] = true 
    else 
        outputDebugString( "Le vehicule '" .. tostring( value ) .. " n existe pas." ) 
    end 
end 
vehicles = vehicles2 
vehicles2 = nil 
  
local function isJobVehicle( vehicle ) 
    return vehicle and vehicles[ getElementModel( vehicle ) ] and not exports.vehicles:getOwner( vehicle ) or false 
end 
  
-- 
  
local p = { } 
  
local function advanceRoute( player, earnings ) 
    if p[ player ] and p[ player ].route and p[ player ].checkpoint then 
        local cp = p[ player ].checkpoint + 1 
        local c = p[ player ].route[ cp ] 
        local n = p[ player ].route[ cp + 1 ] 
        if c then 
            if p[ player ].route[ p[ player ].checkpoint ] then 
                if p[ player ].route[ p[ player ].checkpoint ].stop and earnings and earnings > 0 then 
                    exports.players:giveMoney( player, earnings ) 
                end 
                 
                if p[ player ].route[ p[ player ].checkpoint ].hint then 
                    triggerClientEvent( player, "gui:hint", player, "Votre job : Chauffeur de bus", p[ player ].route[ p[ player ].checkpoint ].hint ) 
                end 
            end 
             
            if not n then 
                -- last checkpoint 
                triggerClientEvent( player, getResourceName( resource ) .. ":set", player, c.x, c.y, c.z, c.stop and true ) 
            else 
                triggerClientEvent( player, getResourceName( resource ) .. ":set", player, c.x, c.y, c.z, c.stop and true, n.x, n.y, n.z, n.stop and true ) 
            end 
        else 
            if earnings then 
                exports.players:giveMoney( player, earnings * 3 ) 
            end 
            triggerClientEvent( player, getResourceName( resource ) .. ":set", player ) 
            p[ player ] = nil 
            return false 
        end 
        p[ player ].checkpoint = cp 
        return true 
    end 
end 
  
local function panel( player ) 
    if "job-" .. tostring( exports.players:getJob( player ) ) == getResourceName( resource ) then 
        triggerClientEvent (source, "fenetreBUS", getRootElement()) 
    end 
end 
addCommandHandler("bus", panel) 
  
local function newRoute( player ) 
    p[ player ].vehicleOnResourceStart = nil 
    local route = math.random(1, #routes[getElementData(player,"route")] ) 
    p[ player ].route = routes[getElementData(player,"route")][ route ] 
    p[ player ].checkpoint = 0 
    
    advanceRoute( player ) 
end 
  
addEventHandler( "onVehicleEnter", root, 
    function( player, seat ) 
        if seat == 0 and isJobVehicle( source ) then 
            if not p[ player ] then 
                p[ player ] = { } 
            end 
             
            if not p[ player ].route then 
                panel( player ) 
                outputChatBox( "(( /bus  pour commencer votre tournée. ))", player, 255, 0, 0 ) 
            else 
                triggerClientEvent( player, getResourceName( resource ) .. ":show", player ) 
            end 
        end 
    end 
) 
  
  
-- 
  
addEventHandler( "onResourceStart", resourceRoot, 
    function( ) 
        if not routes or routes == 0 then 
            cancelEvent( ) 
            return 
        end 
         
        -- 
         
        setElementData( resourceRoot, "delay", delay ) 
         
        -- 
         
        for key, value in ipairs( getElementsByType( "player" ) ) do 
            local vehicle = getPedOccupiedVehicle( value ) 
            if vehicle and getPedOccupiedVehicleSeat( value ) == 0 and isJobVehicle( vehicle ) then 
                p[ value ] = { vehicleOnResourceStart = vehicle } 
            end 
        end 
         
        setTimer( 
            function( ) 
                for key, value in pairs( p ) do 
                    p[ key ].vehicleOnResourceStart = nil 
                end 
            end, 10000, 1 
        ) 
    end 
) 
  
addEvent( getResourceName( resource ) .. ":ready", true ) 
addEventHandler( getResourceName( resource ) .. ":ready", root, 
    function( ) 
        if source == client then 
            if p[ source ] and getPedOccupiedVehicle( source ) == p[ source ].vehicleOnResourceStart and getPedOccupiedVehicleSeat( source ) == 0 then 
                panel( source ) 
            end 
        end 
    end 
) 
  
-- 
  
addEvent( getResourceName( resource ) .. ":complete", true ) 
addEventHandler( getResourceName( resource ) .. ":complete", root, 
    function( ) 
        if source == client then 
            local vehicle = getPedOccupiedVehicle( source ) 
            if p[ source ] and p[ source ].route and isJobVehicle( vehicle ) and getPedOccupiedVehicleSeat( source ) == 0 then 
                -- distance check 
                if not p[ source ].route[ p[ source ].checkpoint ].stop or getDistanceBetweenPoints2D( p[ source ].route[ p[ source ].checkpoint ].x, p[ source ].route[ p[ source ].checkpoint ].y, getElementPosition( vehicle ) ) < 5 then 
                    local health = math.min( 1000, getElementHealth( vehicle ) ) 
                    if health > 350 then 
                        -- calculate earnings based on vehicle health 
                        local earnings = p[ source ].route[ p[ source ].checkpoint ].stop and math.ceil( ( health - 350 ) / 650 * max_earnings ) or 0 
                         
                        -- get a checkpoint 
                        advanceRoute( source, earnings ) 
                    end 
                end 
            end 
        end 
    end 
) 
  
addEventHandler( "onCharacterLogout", root, 
    function( ) 
        if p[ source ] and p[ source ].route then 
            triggerClientEvent( source, getResourceName( resource ) .. ":set", source ) 
        end 
        p[ source ] = nil 
    end 
) 
  
addEventHandler( "onPlayerQuit", root, 
    function( ) 
        p[ source ] = nil 
    end 
) 
  
-- 
  
addEventHandler( "onVehicleStartEnter", root, 
    function( player, seat ) 
        if seat == 0 and isJobVehicle( source ) and "job-" .. tostring( exports.players:getJob( player ) ) ~= getResourceName( resource ) then 
            outputChatBox( "(( Vous pouvez avoir ce job à l'hôtel de ville. ))", player, 255, 0, 0 ) 
            cancelEvent( ) 
        end 
    end 
) 
  
-- 
  
function introduce( player ) 
    triggerClientEvent( player, getResourceName( resource ) .. ":introduce", player ) 
end 
  

bus_c.lua

local localPlayer = getLocalPlayer( ) 
local position = nil 
local marker = nil 
local blip = nil 
local blip2 = nil 
local wait = false 
local screenX, screenY = guiGetScreenSize( ) 
  
function drawWaitingText( ) 
    -- Voyons si le bus attend 
    local text = "patientez..." 
    local vehicle = getPedOccupiedVehicle( localPlayer ) 
    if vehicle then 
        if getElementHealth( vehicle ) <= 350 then 
            text = "Réparez votre véhicule" 
        elseif wait and wait ~= 0 then 
            local diff = wait - getTickCount( ) 
            if diff >= 0 then 
                text = ( "Veuillez attendre %.1f secondes" ):format( diff / 1000 ) 
            else 
                triggerServerEvent( getResourceName( resource ) .. ":complete", localPlayer ) 
                wait = 0 
            end 
        end 
         
        -- draw the text 
        dxDrawText( text, 4, 4, screenX, screenY, tocolor( 0, 0, 0, 255 ), 1, "pricedown", "center", "center" ) 
        dxDrawText( text, 0, 0, screenX, screenY, tocolor( 255, 255, 255, 255 ), 1, "pricedown", "center", "center" ) 
    end 
end 
  
-- 
  
function fenetreBUS(source) 
    fenetre = guiCreateWindow(521,520,253,122,"Choix de la route",false) 
    guiWindowSetSizable(fenetre,false) 
    label = guiCreateLabel(12,26,232,30,"Vous avez le choix entre 2 routes a faire :\nLa ville ou les villages",false,fenetre) 
    cityButton = guiCreateButton(31,78,88,24,"Ville",false,fenetre) 
    countryButton = guiCreateButton(130,78,88,24,"Villages",false,fenetre) 
    guiSetVisible(fenetre, true)  
    showCursor(true) 
end 
  
    function onGUIClick() 
    if (source == cityButton) then 
    setElementData(localPlayer,"route","city") 
    elseif (source == countryButton) then 
    setElementData(localPlayer,"route","country") 
         end 
    end 
    addEventHandler("onClientGUIClick",root,onGUIClick) 
  
addEvent("fenetreBUS", true) 
addEventHandler("fenetreBUS", getResourceRootElement(getThisResource()), fenetreBUS) 
  
-- 
  
local function hide( ) 
    if isElement( blip ) then 
        destroyElement( blip ) 
    end 
    blip = nil 
     
    if isElement( blip2 ) then 
        destroyElement( blip2 ) 
    end 
    blip2 = nil 
     
    if isElement( marker ) then 
        destroyElement( marker ) 
    end 
    marker = nil 
     
    if wait then 
        wait = false 
        removeEventHandler( "onClientRender", root, drawWaitingText ) 
    end 
end 
  
local function show( ) 
    hide( ) 
     
    if position then 
        -- in lack of a more detailed close-location (aka we use the entrance), use large markers 
        marker = createMarker( position.x, position.y, position.z, "checkpoint", 3, position.stop and 0 or 255, 255, 0, 63 ) 
        if marker then 
            blip = createBlipAttachedTo( marker, 0, 2, position.stop and 0 or 255, 255, 0, 255 ) 
        end 
         
        if position.nx and position.ny and position.nz then 
            blip2 = createBlip( position.nx, position.ny, position.nz, 0, 1, position.nstop and 0 or 255, 255, 0, 255 ) 
        end 
    end 
end 
  
addEvent( getResourceName( resource ) .. ":show", true ) 
addEventHandler( getResourceName( resource ) .. ":show", localPlayer, show ) 
  
addEvent( getResourceName( resource ) .. ":set", true ) 
addEventHandler( getResourceName( resource ) .. ":set", localPlayer, 
    function( x, y, z, stop, nx, ny, nz, nstop ) 
        if x and y and z then 
            position = { x = x, y = y, z = z, stop = stop, nx = nx, ny = ny, nz = nz, nstop = nstop } 
            show( ) 
        else 
            position = nil 
            hide( ) 
        end 
    end 
) 
  
addEventHandler( "onClientPlayerVehicleExit", localPlayer, hide ) 
  
addEventHandler( "onClientResourceStart", resourceRoot, 
    function( ) 
        triggerServerEvent( getResourceName( resource ) .. ":ready", localPlayer ) 
    end 
) 
  
addEventHandler( "onClientMarkerHit", resourceRoot, 
    function( element, matching ) 
        if matching and element == localPlayer then 
            if position.stop then 
                if not wait then 
                    wait = getTickCount( ) + getElementData( resourceRoot, "delay" ) * 1000 
                    addEventHandler( "onClientRender", root, drawWaitingText ) 
                end 
            else 
                triggerServerEvent( getResourceName( resource ) .. ":complete", localPlayer ) 
            end 
        end 
    end 
) 
  
addEventHandler( "onClientMarkerLeave", resourceRoot, 
    function( element, matching ) 
        if matching and element == localPlayer then 
            wait = false 
            removeEventHandler( "onClientRender", root, drawWaitingText ) 
            exports.gui:hint( "Votre job : Chauffeur de bus", "Pas trop vite, laissez le temps aux clients d'entrer !", 3 ) 
        end 
    end 
) 
  

Link to comment

Thanks, most of the things work but I still have this "attempt to get length of field '?' (a nil value)" error at line

local route = math.random( #routes[getElementData(player,"route")] )

(I'll test if it's not the S at route) Not that :P

EDIT : It works ! (But why when I click on "city" it put me the country line ?)

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...