Sign in to follow this  
Stylez

Some help needed

Recommended Posts

Hey guys, can someone help me with this. I need to check if entered carID matches the carID in the carsTable. when i type /v buy its says bad car id...

carsTable = { [415] = 100, [413] = 50 } 
 carShopMarker = createMarker( -2424.12036, -609.69904, 131.62, "cylinder", 1.8, 130, 180, 0, 50 ) 
    
function buyCar(thePlayer, cmd, cmdType, carID) 
    if isElementWithinMarker(thePlayer, carShopMarker) and cmdType == "buy" then 
        for i,v in pairs(carsTable) do 
            if carID == i then 
                --local playerMoney = getPlayerMoney(thePlayer) 
                --if playerMoney <= v then 
                  outputChatBox("good  car id") 
                --end 
           else 
                  outputChatBox("bad car id") 
           end 
       end 
    end 
 end 
 addCommandHandler("v", buyCar) 

Share this post


Link to post

That's because "carID" returns a string, you must convert it to number using tonumber.

Also, what's the point of the loop? you can just do:

if ( carsTable [ tonumber ( carID ) ] ) then 
end 

Share this post


Link to post

but look, now.. i tried to make it if player has enough money to be able to buy a car.. now error says line 9 attempt to compare nil with number. cant understand...

carsTable = { [415] = 100, [413] = 50 } 
carShopMarker = createMarker( -2424.12036, -609.69904, 131.62, "cylinder", 1.8, 130, 180, 0, 50 ) 
  
function buyCar(thePlayer, cmd, cmdType, carID) 
    if isElementWithinMarker(thePlayer, carShopMarker) and cmdType == "buy" then 
        if (carsTable[tonumber(carID)]) then 
            for i,v in pairs(carsTable) do 
                local playerMoney = getPlayerMoney(thePlayer) 
                if playerMoney > carsTable[v] then 
                    outputChatBox("nigga got a car") 
                else 
                    outputChatBox("bbz, arba ner pinige") 
                end 
            end 
        else 
            outputChatBox("Neteisingas Masinos ID", thePlayer) 
        end 
    end 
end 
addCommandHandler("v", buyCar) 

Share this post


Link to post
carsTable = { [415] = 100, [413] = 50 } 
carShopMarker = createMarker( -2424.12036, -609.69904, 131.62, "cylinder", 1.8, 130, 180, 0, 50 ) 
  
function buyCar ( thePlayer, cmd, cmdType, carID ) 
    if isElementWithinMarker ( thePlayer, carShopMarker ) and cmdType == "buy" then 
        local price = carsTable [ tonumber ( carID ) ] 
        if ( price ) then 
            local playerMoney = getPlayerMoney ( thePlayer ) 
            if ( playerMoney >= price ) then 
                outputChatBox ( ":O got a car" ) 
            else 
                outputChatBox ( "bbz, arba ner pinige" ) 
            end 
        else 
            outputChatBox ( "Neteisingas Masinos ID", thePlayer ) 
        end 
    end 
end 
addCommandHandler ( "v", buyCar ) 

Share this post


Link to post

No, you see carsTable = { [415] = 100, [413] = 50 } the 100 and 50 are prices for cars..

Share this post


Link to post

So? my script will check if the vehicle is on the table, if so, then it'll check if you have enough money.

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.