Dzsozi (h03) Posted November 21, 2016 Share Posted November 21, 2016 (edited) Hi! I would like to do a little script for my server, where I can give custom names and a new thing, brands for vehicles, and within this script I would like to replace vehicle mods as well, if there is any. The problem is that I don't know how to get the things inside a table that is inside a table. Let me show my script to you and you will understand: vehicleTable = { ["Maibatsu"] = { {vehicleName="Penumbra", fileName="mods/penumbra", modelID=436}, }, ["Benefactor"] = { {vehicleName="Schwartzer", fileName="mods/schwartzer", modelID=445}, }, } Here is the table for brands. Inside the vehicleTable I have created a new table for the vehicles with the brands. What I would like to do is replace the vehicle models (model ids are given) but I don't know how to get this variable, how to loop that, how to call back that, I don't know how to say or what it is called, here's what I tried, but it's not working: function loadMods() for index, mod in pairs(vehicleTable) do txd = engineLoadTXD ( vehicleTable[index].fileName .. ".txd" ) engineImportTXD ( txd, vehicleTable[index].modelID ) dff = engineLoadDFF ( vehicleTable[index].fileName .. ".dff", vehicleTable[index].modelID ) engineReplaceModel ( dff, vehicleTable[index].modelID ) end end addEventHandler("onClientResourceStart",resourceRoot, function () loadMods() end) Yes, these are in two different scripts and yes, they are in the same resource, and the script with the table is marked as a "shared" type, so there is no problem with that, I just don't know how to get the filename and the modelid variables inside a table which is inside a table. Also, how can I make an export function for getting the custom vehicle name and brand of the vehicle? EDIT: I get this error in debugscript: replaceC.lua:3 attempt to concatenate field 'fileName' (a nil value) I don't know what does that mean. Edited November 21, 2016 by Dzsozi Link to comment
Addlibs Posted November 21, 2016 Share Posted November 21, 2016 function loadMods() for brandIndex, brandCars in pairs(vehicleTable) do -- loop through brands for index, mod in pairs(brandCars) do -- loop through cars of the brand you're looping through right now -- either txd = engineLoadTXD ( vehicleTable[brandIndex][index].fileName .. ".txd" ) -- or txd = engineLoadTXD ( brandCars[index].fileName .. ".txd" ) -- or txd = engineLoadTXD ( mod.fileName .. ".txd" ) engineImportTXD ( txd, vehicleTable[index].modelID ) dff = engineLoadDFF ( vehicleTable[index].fileName .. ".dff", vehicleTable[index].modelID ) engineReplaceModel ( dff, vehicleTable[index].modelID ) end end end If you have a table named vehicles, and every value inside is a table brand containing every car of that brand, then you can simply loop through vehicles, and for every brand in that table, loop through its cars. That is, loop through the table and then loop through every of the values of that table. Link to comment
Dzsozi (h03) Posted November 21, 2016 Author Share Posted November 21, 2016 Thank you for your quick answer, you helped me alot right now! I would like to ask for help with one more thing. How can I do that function I was talking about? So for example I have a function like getVehicleBrand(vehicle) and how can I return the brand of the given vehicle? And the same with the vehicle name? How can I return the custom vehicle name of a given vehicle? Link to comment
Dzsozi (h03) Posted November 22, 2016 Author Share Posted November 22, 2016 Help please. Link to comment
LoPollo Posted November 22, 2016 Share Posted November 22, 2016 I'm in a hurry so i hope i did not misunderstood or not read something. If in the script you have acces to the vehicleTable you can loop the vehicleTable for k,v in pairs(vehicleTable) do for i=1,#v do if v[i].modelID == getElementModel(theVehicle) then --example: k: "Maibatsu", v[i].vehicleName: "Penumbra" break end end end untested if you will be calling this function lots of times and performance matters you could create a variable (eg lookupTable), call function like the above to create the table in lookuptable and read this lookuptable everytime you need. that table structure should be like this: variable[theVehicleID] = {brand="someBrand", vehicleName="someName"} Link to comment
Dzsozi (h03) Posted November 22, 2016 Author Share Posted November 22, 2016 (edited) Thank you, I managed to do it! This script works well, I get the custom name of the model and the brand from the table, but regarding the performance, is this "correct"? Or is there any better way to do this? function getVehicleBrand(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if not tonumber(vehicle) and not tostring(vehicle) then if v[i].modelID == getElementModel(vehicle) then return tostring(k) end elseif tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return tostring(k) end elseif tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) or tostring(v[i].vehicleName) == tostring(vehicle) then return tostring(k) end else return false end end end end function getVehicleCustomName(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if not tonumber(vehicle) and not tostring(vehicle) then if v[i].modelID == getElementModel(vehicle) then return tostring(v[i].vehicleName) end elseif tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return tostring(v[i].vehicleName) end elseif tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) or tostring(v[i].vehicleName) == tostring(vehicle) then return tostring(v[i].vehicleName) end else return false end end end end outputChatBox(getVehicleBrand("Stallion") .. ", " .. getVehicleCustomName(439)) Edited November 22, 2016 by Dzsozi Link to comment
Dzsozi (h03) Posted November 22, 2016 Author Share Posted November 22, 2016 One more thing I can't fix. I don't have every vehicle in the table, how can I make a function inside these two functions (getVehicleCustomName and getVehicleBrand) to check if the given vehicle is inside a table or not, and if it is not then return the default name of the vehicle and for the brand return it to an empty string. Hope you understand what I mean. So for example I have the stallion inside the table, under the Imponte brand and it's name is Dukes, but I don't have the bravura inside the table. I am trying to make a hud display when I enter a vehicle the custom name and the brand of the vehicle is being displayed, but if I enter a vehicle that is not inside the table I would like to get the default vehicle name of the vehicle and for the brand, if there is no brand for the vehicle an empty string like "". Here's my current script with the export functions: function getVehicleBrand(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if v[i].modelID ~= nil then if tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return k end elseif tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) or tostring(v[i].vehicleName) == tostring(vehicle) then return k end else return "" end else return "" end end end end function getVehicleCustomName(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if v[i].modelID ~= nil then if tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return v[i].vehicleName end elseif tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) or tostring(v[i].vehicleName) == tostring(vehicle) then return v[i].vehicleName end else return getVehicleNameFromModel(vehicle) end else return getVehicleNameFromModel(vehicle) end end end end Link to comment
LoPollo Posted November 22, 2016 Share Posted November 22, 2016 (edited) 4 hours ago, Dzsozi said: Thank you, I managed to do it! This script works well, I get the custom name of the model and the brand from the table, but regarding the performance, is this "correct"? Or is there any better way to do this? function getVehicleBrand(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if not tonumber(vehicle) and not tostring(vehicle) then if v[i].modelID == getElementModel(vehicle) then return tostring(k) end elseif tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return tostring(k) end elseif tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) or tostring(v[i].vehicleName) == tostring(vehicle) then return tostring(k) end else return false end end end end function getVehicleCustomName(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if not tonumber(vehicle) and not tostring(vehicle) then if v[i].modelID == getElementModel(vehicle) then return tostring(v[i].vehicleName) end elseif tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return tostring(v[i].vehicleName) end elseif tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) or tostring(v[i].vehicleName) == tostring(vehicle) then return tostring(v[i].vehicleName) end else return false end end end end outputChatBox(getVehicleBrand("Stallion") .. ", " .. getVehicleCustomName(439)) Do not return false inside the loop, it will break it. If the function must return false if the vehicle is not found, that means that the whole table has been looped, and no item was the one you were looking for. so you must use the "return false" after the loop finish, when all vehicles in the table have been compared. Also i don't get this: if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) or tostring(v[i].vehicleName) == tostring(vehicle) then return v[i].vehicleName end --tostring(v[i].vehicleName) == tostring(vehicle) then return vehiclename???? --if tostring(vehicle) is equal to vehiclename the script already have it, why should you call this function? 3 hours ago, Dzsozi said: check if the given vehicle is inside a table or not, and if it is not then return the default name of the vehicle and for the brand return it to an empty string. vehName: if all the table has been iterated and no item was the one you were looking for, then return getVehicleNameFromModel (after you have read what written above, basically just replace false with the name if 1. the vehicle arg was a vehicle element: get the name and return it (if it's not a vehicle element then return false) or 2. the vehicle arg was a number, return what getVehNameFromModel returns (false if not valid, name if valid - exaclty what u need)) or 3. the vehicle arg was a string, convert to number (if not valid number return false) and see No. 2 ) brand: once you're outside the loop (that means no vehicle was found) return empty string Edited November 22, 2016 by LoPollo Link to comment
Dzsozi (h03) Posted November 22, 2016 Author Share Posted November 22, 2016 I really don't understand what am I doing wrong, I tried it again, it's different, I tried doing what you wrote, but it's not working. It only outputs Enus Huntley, but it is wrong as well, because I am creating a bus in the script and it should output Brute, Bus, for the number and string arguments it doesn't even output anything, I just get this error in the debugscript: 'brandG.lua:202: attempt to concatenate a boolean value I don't understand what's wrong, I can't fix it. This is my whole current script: vehicleTable = { -- brand --> vehicle -- name, mod (if there is any), model id ["Maibatsu"] = { {vehicleName="Penumbra", fileName="mods/previon", modelID=436}, {vehicleName="Manchez", fileName="mods/sanchez", modelID=468}, }, ["Benefactor"] = { {vehicleName="Schwartzer", fileName="mods/elegant", modelID=507}, {vehicleName="Schafter", fileName="mods/admiral", modelID=445}, }, ["Pegassi"] = { {vehicleName="Infernus", fileName="mods/infernus", modelID=411}, }, ["Enus"] = { {vehicleName="Huntley", fileName="mods/huntley", modelID=579}, {vehicleName="Cognoscenti", modelID=580}, }, ["Declasse"] = { {vehicleName="Rancher", modelID=489}, {vehicleName="Rancher XL", fileName="mods/intruder", modelID=546}, {vehicleName="Tampa", fileName="mods/tampa", modelID=549}, {vehicleName="Perennial", fileName="mods/peren", modelID=404}, {vehicleName="Voodoo", fileName="mods/voodoo", modelID=412}, {vehicleName="Tornado", modelID=576}, {vehicleName="Savanna", modelID=567}, {vehicleName="Moonbeam", fileName="mods/moonbeam", modelID=418}, }, ["BF"] = { {vehicleName="Injection", modelID=424}, {vehicleName="Bandito", fileName="mods/bandito", modelID=568}, }, ["Annis"] = { {vehicleName="Stratum", modelID=561}, {vehicleName="Elegy", modelID=562}, }, ["Karin"] = { {vehicleName="Manana SS", fileName="mods/emperor", modelID=585}, {vehicleName="Manana", fileName="mods/manana", modelID=410}, {vehicleName="Sultan", fileName="mods/sultan", modelID=560}, {vehicleName="Kuruma", fileName="mods/primo", modelID=547}, }, ["Mammoth"] = { {vehicleName="Patriot", fileName="mods/patriot", modelID=470}, {vehicleName="Hydra", fileName="mods/hydra", modelID=520}, {vehicleName="Skimmer", modelID=460}, }, ["Lampadati"] = { {vehicleName="Super GT", fileName="mods/supergt", modelID=506}, }, ["Imponte"] = { {vehicleName="Dukes", fileName="mods/stallion", modelID=439}, {vehicleName="Centauro S-200", fileName="mods/phoenix", modelID=603}, }, ["Grotti"] = { {vehicleName="Turismo R", modelID=451}, }, ["Willard"] = { {vehicleName="Majestic", modelID=517}, {vehicleName="Tahoma", fileName="mods/tahoma", modelID=566}, {vehicleName="Polaris V8", fileName="mods/solair", modelID=458}, }, ["Truffade"] = { {vehicleName="Deluxo", fileName="mods/cadrona", modelID=527}, {vehicleName="Hustler", fileName="mods/hustler", modelID=545}, }, ["Vapid"] = { {vehicleName="Contender", fileName="mods/yosemite", modelID=554}, {vehicleName="Contender XL", fileName="mods/landstal", modelID=400}, {vehicleName="Hotknife", fileName="mods/hotknife", modelID=434}, }, ["Brute"] = { {vehicleName="Bus", modelID=431}, {vehicleName="Dashound", fileName="mods/coach", modelID=437}, {vehicleName="Ambulance", modelID=416}, {vehicleName="Enforcer", modelID=427}, {vehicleName="Boxville", modelID=498}, {vehicleName="Journey", modelID=508}, {vehicleName="Pony", fileName="mods/pony", modelID=413}, {vehicleName="Hot-Dog", modelID=588}, {vehicleName="Stockade", modelID=428}, {vehicleName="Barracks", modelID=433}, {vehicleName="Towtruck", modelID=525}, {vehicleName="Utility Van", modelID=552}, {vehicleName="Firetruck Ladder", modelID=544}, }, ["HVY"] = { {vehicleName="Linerunner", modelID=403}, {vehicleName="Roadtrain", modelID=515}, {vehicleName="Tanker", modelID=514}, {vehicleName="Benson", modelID=499}, {vehicleName="Dozer", modelID=486}, {vehicleName="Flatbed", modelID=455}, {vehicleName="Yankee", fileName="mods/yankee", modelID=456}, {vehicleName="Packer", modelID=443}, {vehicleName="Mule", modelID=414}, {vehicleName="Dumper", modelID=406}, {vehicleName="Firetruck", fileName="mods/firetruk", modelID=407}, {vehicleName="Trashmaster", fileName="mods/trash", modelID=408}, {vehicleName="Cement Truck", modelID=524}, {vehicleName="Combine Harvester", modelID=532}, {vehicleName="DFT-30", modelID=578}, {vehicleName="Forklift", modelID=530}, }, ["Cheval"] = { {vehicleName="Picador", fileName="mods/picador", modelID=600}, }, ["Albany"] = { {vehicleName="Buccaneer", fileName="mods/buccanee", modelID=518}, }, ["Military"] = { {vehicleName="Rhino Tank", fileName="mods/rhino", modelID=432}, {vehicleName="Hunter", modelID=425}, }, } function getVehicleBrand(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if vehicle and isElement(vehicle) then return k else return false end if tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) then return k end else return false end if tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return k end else return false end end end return "" end function getVehicleCustomName(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if v[i].vehicleName then if vehicle and isElement(vehicle) then return v[i].vehicleName else return false end if tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) then return v[i].vehicleName end else return false end if tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return v[i].vehicleName end else return false end else return getVehicleNameFromModel(vehicle) end end end end local car = createVehicle(431, 210.8377532959, -1466.6046142578, 10.860450744629) outputChatBox(getVehicleBrand(car) .. ", " .. getVehicleCustomName(car)) outputChatBox(getVehicleBrand(400) .. ", " .. getVehicleCustomName(400)) outputChatBox(getVehicleBrand("Cadrona") .. ", " .. getVehicleCustomName("Cadrona")) Link to comment
LoPollo Posted November 22, 2016 Share Posted November 22, 2016 --if #1 if vehicle and isElement(vehicle) then return k else return false end --if #2 if tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) then return k end else return false end If we insert a string as "vehicle" (we want to execute if #2) we must execute if #1 first. if vehicle is a string, it is a value (if vehicle will return true) but it's not an element since it's a string (so if isElement(vehicle) will return false) this means that once the "return false" is executed, the function will end, and we will never reach if #2 You should use something like this instead Spoiler brand: --original function: function getVehicleBrand(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if vehicle and isElement(vehicle) then return k else return false end if tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) then return k end else return false end if tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return k end else return false end end end return "" end --edited function: function getVehicleBrand(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if vehicle then if isElement(vehicle) then --case element if getElementType(vehicle) == "vehicle" then if getElementModel( vehicle ) == v[i].modelID then return k end else return false --if it's an element but it's not a veh, we can kill the function... it's not a vehicle and that's not we handle here. It's junk :D end elseif tostring(vehicle) then --case string if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) then return k end if not getVehicleModelFromName( vehicle ) then --it's just a general check, if it's not a valid name it's junk we won't need return false end elseif tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return k end if getVehicleNameFromModel( tonumber(vehicle) ) then return false --another general check: if the number provided is NOT an ID it is again junk end end --do not return false here, otherwise the loop will break else return false --our arg were nil or false end end end return "" end veh name: --orginal func: function getVehicleCustomName(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if v[i].vehicleName then if vehicle and isElement(vehicle) then return v[i].vehicleName else return false end if tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) then return v[i].vehicleName end else return false end if tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return v[i].vehicleName end else return false end else return getVehicleNameFromModel(vehicle) end end end end --edited func: function getVehicleCustomName(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if vehicle then if isElement(vehicle) then if getElementType( vehicle ) == "vehicle" then if getElementModel( vehicle ) == v[i].modelID then return v[i].vehicleName end end elseif tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) then return v[i].vehicleName end if not getVehicleModelFromName( vehicle ) then --it's just a general check, if it's not a valid name it's junk we won't need return false end elseif tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return v[i].vehicleName end if getVehicleNameFromModel( tonumber(vehicle) ) then return false --another general check: if the number provided is NOT an ID it is again junk end end else return false --our arg were nil or false end end end --return default name... let's do it --note: it's possible to compact this, but it will be much less readable if isElement( vehicle ) and getElementType( vehicle ) == "vehicle" then return getVehicleNameFromModel( getElementModel( vehicle ) ) --getVehicleName( vehicle ) lol elseif tostring(vehicle) then --if it wasn't valid the functions already have returned false, so if it's a string in this part of code it's a valid name return vehicle elseif tonumber(vehicle) then --if it's a number here... it's a valid model so get the name return getVehicleNameFromModel( vehicle ) else return nil --in case i forgot something, nil will be returned. remember that both nil and false are "false values", but they are different! end end both are untested, also passing so many argument types as you can see makes the function much longer (however we could have made a better-styled, more compact function even in our scenario) and can also lead to confusion. Let me know if it works and if you can fix it Link to comment
Dzsozi (h03) Posted November 23, 2016 Author Share Posted November 23, 2016 These are working if the vehicle is a string or an element, but if I put a number there I get the ' attempt to concatenate a boolean value ' error in debugscript. I have read through the script you posted, I tried to fix it but it does not work for some reason and I can't fix it. It only works with an element and a string. function getVehicleBrand(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if vehicle then if isElement(vehicle) then --case element if getElementType(vehicle) == "vehicle" then if getElementModel( vehicle ) == v[i].modelID then return k end else return false --if it's an element but it's not a veh, we can kill the function... it's not a vehicle and that's not we handle here. It's junk :D end elseif tostring(vehicle) then --case string if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) then return k end if not getVehicleModelFromName( vehicle ) then --it's just a general check, if it's not a valid name it's junk we won't need return false end elseif tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return k end if getVehicleNameFromModel( tonumber(vehicle) ) then return false --another general check: if the number provided is NOT an ID it is again junk end end --do not return false here, otherwise the loop will break else return false --our arg were nil or false end end end return "" end function getVehicleCustomName(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if vehicle then if isElement(vehicle) then if getElementType( vehicle ) == "vehicle" then if getElementModel( vehicle ) == v[i].modelID then return v[i].vehicleName end end elseif tostring(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) then return v[i].vehicleName end if not getVehicleModelFromName( vehicle ) then --it's just a general check, if it's not a valid name it's junk we won't need return false end elseif tonumber(vehicle) then if v[i].modelID == tonumber(vehicle) then return v[i].vehicleName end if getVehicleNameFromModel( tonumber(vehicle) ) then return false --another general check: if the number provided is NOT an ID it is again junk end end else return false --our arg were nil or false end end end --return default name... let's do it --note: it's possible to compact this, but it will be much less readable if isElement( vehicle ) and getElementType( vehicle ) == "vehicle" then return getVehicleNameFromModel( getElementModel( vehicle ) ) --getVehicleName( vehicle ) lol elseif tostring(vehicle) then --if it wasn't valid the functions already have returned false, so if it's a string in this part of code it's a valid name return vehicle elseif tonumber(vehicle) then --if it's a number here... it's a valid model so get the name return getVehicleNameFromModel( vehicle ) else return nil --in case i forgot something, nil will be returned. remember that both nil and false are "false values", but they are different! end end local car = createVehicle(516, 210.8377532959, -1466.6046142578, 10.860450744629) outputChatBox(getVehicleBrand(516) .. ", " .. getVehicleCustomName(516)) outputChatBox(getVehicleCustomName(car)) outputChatBox(getVehicleBrand("Cadrona") .. " " .. getVehicleCustomName("Cadrona")) Link to comment
pa3ck Posted November 23, 2016 Share Posted November 23, 2016 It might be the order. Because a number can be string and number but a string can't be a number. So, try to change the order. Put the elseif tostring(vehicle) under the tonumber(vehicle) 1 Link to comment
Dzsozi (h03) Posted November 23, 2016 Author Share Posted November 23, 2016 It is the same. Link to comment
LoPollo Posted November 23, 2016 Share Posted November 23, 2016 (edited) Uuuups tostring(vehicle) will convert the element into a string representation, even a mta element converted to string is like userdata: [someNumbers] or element: [someOtherNumber].... so we shoud check if the tostring(vehicle) is equal to vehicle (the string equivalent of the elòement is the element itself: so we're checking if the element IS a string, and not if it's possible to convert it to string - lol) replace tostring(vehicle) with tostring(vehicle) == vehicle Note: untested Edited November 23, 2016 by LoPollo Link to comment
Dzsozi (h03) Posted November 23, 2016 Author Share Posted November 23, 2016 I have managed to fix it, tostring(vehicle) == vehicle wasn't working, but something came into my mind, here's the final result if you are interested: function getVehicleBrand(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if vehicle then if isElement(vehicle) then --case element if getElementType(vehicle) == "vehicle" then if getElementModel( vehicle ) == v[i].modelID then return k end else return false --if it's an element but it's not a veh, we can kill the function... it's not a vehicle and that's not we handle here. It's junk :D end elseif tonumber(vehicle) and not tostring(vehicle) then if v[i].modelID == tonumber(vehicle) then return k end if getVehicleNameFromModel( tonumber(vehicle) ) then return false --another general check: if the number provided is NOT an ID it is again junk end elseif tostring(vehicle) and not tonumber(vehicle) then --case string if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) then return k end if not getVehicleModelFromName( vehicle ) then --it's just a general check, if it's not a valid name it's junk we won't need return false end end --do not return false here, otherwise the loop will break else return false --our arg were nil or false end end end return "" end function getVehicleCustomName(vehicle) for k,v in pairs(vehicleTable) do for i=1,#v do if vehicle then if isElement(vehicle) then if getElementType( vehicle ) == "vehicle" then if getElementModel( vehicle ) == v[i].modelID then return v[i].vehicleName end end elseif tonumber(vehicle) and not tostring(vehicle) then if v[i].modelID == tonumber(vehicle) then return v[i].vehicleName end if getVehicleNameFromModel( tonumber(vehicle) ) then return false --another general check: if the number provided is NOT an ID it is again junk end elseif tostring(vehicle) and not tonumber(vehicle) then if getVehicleNameFromModel(v[i].modelID) == tostring(vehicle) then return v[i].vehicleName end if not getVehicleModelFromName( vehicle ) then --it's just a general check, if it's not a valid name it's junk we won't need return false end end else return false --our arg were nil or false end end end --return default name... let's do it --note: it's possible to compact this, but it will be much less readable if isElement( vehicle ) and getElementType( vehicle ) == "vehicle" then return getVehicleNameFromModel( getElementModel( vehicle ) ) --getVehicleName( vehicle ) lol elseif tonumber(vehicle) then --if it's a number here... it's a valid model so get the name return getVehicleNameFromModel( vehicle ) elseif tostring(vehicle) then --if it wasn't valid the functions already have returned false, so if it's a string in this part of code it's a valid name return vehicle else return nil --in case i forgot something, nil will be returned. remember that both nil and false are "false values", but they are different! end end local car = createVehicle(516, 210.8377532959, -1466.6046142578, 10.860450744629) outputChatBox(getVehicleBrand(420) .. " " .. getVehicleCustomName(420) .. " 1") outputChatBox(getVehicleCustomName(car) .. " 2") outputChatBox(getVehicleBrand("Cadrona") .. " " .. getVehicleCustomName("Cadrona") .. " 3") 1 Link to comment
LoPollo Posted November 23, 2016 Share Posted November 23, 2016 Good you fixed it, basically what @pa3ck said... I don't fully understand it since if the arg is a number then the tostring of a number (10) will return the string ("10"), a true value. Negating this in an "and" will make "and false" and thus the function should not work >.> still if it's work i don't see any reason to stay on this also be aware that passing a value that's not an element and not a number will always be processed in the "string" block (for example a table will be processed here)... so if you want to edit the code as i said (i'm talking about handling 3 types of arg... this problem started from here) to get a easier script to read, maintain and edit. But again if you consider this a waste of time since it's working don't mind Link to comment
Dzsozi (h03) Posted November 23, 2016 Author Share Posted November 23, 2016 Alright, thank you all for every help! You helped me so much with this! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now