Jump to content

[HELP] Strange result of a function


Recommended Posts

Hello, I recently made a resource that changes the default car names into real ones. Here is the code:

function findVehicleName() 
    for _, theVehicle in pairs( getElementsByType( "vehicle" ) ) do 
        local car = getVehicleID( theVehicle ) 
        if (car==603) then 
            return "Shelby" 
        end 
        if (car==474) then 
            return "Mercedes E500" 
        end 
        if (car==411) then 
            return "Lamborghini Veneno" 
        end 
        end 
    end 
   -- return " (".. getVehicleName(source) ..") " 
--end 

I don't know why, when I put that "findVehicleName" into other function, it always gets me only the Infernus (Lamborghini Veneno) instead of the right name.

For example if I do something like:

outputChatBox("(( This is a " .. findVehicleName(theVehicle) .. ". ))", thePlayer, 255, 195, 14) 

It will say it is a Lamborghini Veneno(Infernus) even though the car is a Merit or Bullet etc..

Anyone knows why?

Link to comment

[quote name=..:D&G:..]The outputChatBox doesn't show anymore... And no errors

Edited #

addEventHandler ( 'onVehicleEnter' , resourceRoot, 
function ( ) 
for _, theVehicle in pairs( getElementsByType( "vehicle" ) ) do 
local Model = getElementModel ( theVehicle ) 
if ( Model == 603 ) then 
outputChatBox(" This is a Shelby  " , source , 255, 255, 0 ) 
elseif ( Model == 474 ) then 
outputChatBox(" This is a Mercedes E500  " , source , 255, 255, 0 ) 
elseif ( Model == 411 ) then 
outputChatBox(" This is a Lamborghini Veneno  " , source , 255, 255, 0 ) 
    end 
  end 
end 
) 

--ServerSide ,

/debugscript 3

Edited by Guest
Link to comment
That doesn't make any sense.

It'll just spam the chat box with all the vehicle names, also, the loop is wrong, that sort of table can't be looped with "ipairs".

What's the correct ?

Becuase im , not very Good , with tabels ,

Link to comment

[quote name=..:D&G:..]Eh.. f*ck it, I am quiting this script, just doesn't want to work :P I wanted to reopen my old roleplay server but it seems that the world doesn't want me to reopen it :P

Thanks for help anywa!

did you try the new post of mine ?

what erros does it shows ?

Link to comment

[quote name=..:D&G:..]Yes I tried, and it still doesn't show nothing, not even an error...

Hope this works ,

CarNames = {[603] = "Shelby",[474] = "Mercedes E500", [411] = "Lamborghini Veneno"} 
addEventHandler( 'onVehicleEnter', resourceRoot, 
function ( ) 
outputChatBox("You have entered " ..  findVehicleName(getElementModel(source)))  
  end 
) 
  
  
function findVehicleName(theVehicle) 
     return CarNames[theVehicle] 
end 

--ServerSide ,

Link to comment

How exactly do you want to use this...? It would help significantly to write it for you... :P

Edit:

Here's an example of how you can do it, with the script below you can execute a command like this one; "/cvn 411" - this will check if it exists in the table with custom names - if not, it will get the default vehicle name. Example, 579 will return "Huntley".

I just wrote this up out of my head, so there might be some mistake somewhere, but the table check should work. Also I'm not sure what the results are if you try to check an ID like 6000. Assuming it will return false, or nil.

Client-Side;

local vehicleNames =  
{ 
    [411] = "Lamborghini Veneno", 
    [474] = "Mercedes E500", 
    [603] = "Shelby" 
}; 
  
function findVehicleName(theVehicleID) 
    if(type(vehicleNames[theVehicleID]) == "string") then 
        return vehicleNames[theVehicleID]; 
    else 
        return getVehicleNameFromModel(theVehicleID); 
    end 
end 
  
function onClientCheckVehicleName_CMD(theCMD, arg1) 
    if(arg1 ~= nil) then 
        local ID = tonumber(arg1); 
        local theName = findVehicleName(ID); 
        outputChatBox(tostring(theName)); 
    end 
end 
addCommandHandler("CVN", onClientCheckVehicleName_CMD, false, false); 

Link to comment
CarNames = {[603] = "Shelby",[474] = "Mercedes E500", [411] = "Lamborghini Veneno"} 
  
function findVehicleName(theVehicle) 
    if (type(CarNames[getElementModel(theVehicle)]) == "string") then 
        return CarNames[getElementModel(theVehicle)] 
    else 
        return getVehicleNameFromModel(getElementModel(theVehicle)) 
    end 
end 
  
function VehicleEnter(Player) 
    outputChatBox("You entered the " .. findVehicleName(source) .. "!",Player) 
end 
addEventHandler("onVehicleEnter",getRootElement(),VehicleEnter) 

Just try this, if it doesn't work, you are doing something wrong.

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