Jump to content

[HELP] Strange result of a function


Recommended Posts

Posted

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?

76561198066470453.png

SP37Ecj.jpg?2MTA:Rust Pre-Alpha Build v.0.3:SP37Ecj.jpg?2

https://forum.mtasa.com/viewtopic.php?f=114&t=97848

2Pac: ''Only God can judge me!''

Posted

Well, it's because you aren't using the vehicle element passed in the function arguments, you are looping all the vehicles and checking their models.

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted
CarNames = {[603] = "Shelby",[474] = "Mercedes E500", [411] = "Lamborghini Veneno"} 
  
function findVehicleName(theVehicle) 
    return CarNames[getElementModel(theVehicle)] 
end 

My Skype:

scriptmtasupport

My Resources:

http://www.scriptmta.co.nr - Cheap high-quality MTA resources by me and Kevin (ScriptMTA.co.nr)

Posted (edited)

,

Edited by Guest

- New , Kill System

- New, GameMode Intro

- Leve / Exp System

- New nametag showing style

- New , Hud For Players

- Skin Selection from SA-MP

- Money System / Buy Weapons

- Drop Weapons

- New, Flood System

- New , Group Assign

- Gun license For Weapons

- Random Rule System For Money

Posted

Change the event to

  
addEventHandler ( 'onVehicleEnter', resourceRoot, 

- New , Kill System

- New, GameMode Intro

- Leve / Exp System

- New nametag showing style

- New , Hud For Players

- Skin Selection from SA-MP

- Money System / Buy Weapons

- Drop Weapons

- New, Flood System

- New , Group Assign

- Gun license For Weapons

- Random Rule System For Money

Posted (edited)

[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

- New , Kill System

- New, GameMode Intro

- Leve / Exp System

- New nametag showing style

- New , Hud For Players

- Skin Selection from SA-MP

- Money System / Buy Weapons

- Drop Weapons

- New, Flood System

- New , Group Assign

- Gun license For Weapons

- Random Rule System For Money

Posted

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

San Andreas Utopia RPG (SAUR) Owner & Developer.

560x95_FFFFFF_FF9900_000000_000000.png

Education is the most powerful weapon which you can use to change the world.

Posted
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 ,

- New , Kill System

- New, GameMode Intro

- Leve / Exp System

- New nametag showing style

- New , Hud For Players

- Skin Selection from SA-MP

- Money System / Buy Weapons

- Drop Weapons

- New, Flood System

- New , Group Assign

- Gun license For Weapons

- Random Rule System For Money

Posted

[quote name=..:D&G:..]Doesn't work, and no errors... and tbh, I am not a very good scripter, but that function doesn't look right.. :/

OK , Post Edited , Try Now ,

- New , Kill System

- New, GameMode Intro

- Leve / Exp System

- New nametag showing style

- New , Hud For Players

- Skin Selection from SA-MP

- Money System / Buy Weapons

- Drop Weapons

- New, Flood System

- New , Group Assign

- Gun license For Weapons

- Random Rule System For Money

Posted

[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 ?

- New , Kill System

- New, GameMode Intro

- Leve / Exp System

- New nametag showing style

- New , Hud For Players

- Skin Selection from SA-MP

- Money System / Buy Weapons

- Drop Weapons

- New, Flood System

- New , Group Assign

- Gun license For Weapons

- Random Rule System For Money

Posted

[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 ,

- New , Kill System

- New, GameMode Intro

- Leve / Exp System

- New nametag showing style

- New , Hud For Players

- Skin Selection from SA-MP

- Money System / Buy Weapons

- Drop Weapons

- New, Flood System

- New , Group Assign

- Gun license For Weapons

- Random Rule System For Money

Posted

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); 

If I help you in a thread and you need further assistance, please don't PM me - use the thread you created instead. This way everyone on the forum can take advantage of it.

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

My Skype:

scriptmtasupport

My Resources:

http://www.scriptmta.co.nr - Cheap high-quality MTA resources by me and Kevin (ScriptMTA.co.nr)

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