Jump to content

MySQL weird error


Karuzo

Recommended Posts

Hey Guys,

so i wanted to write my Car-system to mysql but i always get a weird error if i want to buy a car.

This is what i get :

ERROR: CarSys\car_h_s.lua:29: attempt to index upvalue 'datas' (a nil value)

This is a part of the code(where the error occurs):

function vehicleCreat( player, preis,vId) 
    local account = tostring(getElementData ( source, "username" )) 
    if connection then 
        local query = dbQuery(connection, "SELECT * FROM wcf1_user WHERE username = '"..account.."'") 
        local result = dbPoll(query,-1) 
        datas = result[1] 
    local hascar = datas.hascar -- this is the line where the error occurs 
    if  hascar == 1 then 
        outputChatBox("Du hast bereits ein Auto!",player, 125,0,0,false) 
    else 
        local money = datas.money 
        if money >= math.abs(preis) then 
            local vName = getVehicleNameFromModel ( vId ) 
            local insert = dbExec ( connection, "INSERT INTO wcf1_user(vehid,carhealth,name,hascar) VALUES(?,?,?,?)", vId, 1000, tostring(vName), 1) 
            vehicle = createVehicle ( vId, 545.33130, -1257.61816, 16.62232, 0, 0, -45 ) 
            warpPedIntoVehicle(player, vehicle) 
            takePlayerMoney ( player, math.abs(preis) ) 
            --setElementData(player, "CarGot",true) 
            outputDebugString("query successfull") 
            outputChatBox("Du hast dir erfolgreich ein  " .. vName.. " gekauft!", player, 0, 125, 0, false) 
        else 
            outputChatBox("Du kannst dir dieses Auto nicht leisten!",player, 125, 0, 0, false) 
            outputDebugString("Query not successfull!") 
        end 
    end 
    end 
end 
addEvent( "CarBuy", true  ) 
addEventHandler( "CarBuy", root, vehicleCreat ) 

hascar is a column where i set the data to 1 if he has a car and to 0 if he doesn't have a car or sold it.

Hope you could help me out with this.

Link to comment
  
function vehicleCreat( player, preis,vId) 
    local account = tostring(getElementData ( source, "username" )) 
    if connection then 
        local query = dbQuery(connection, "SELECT * FROM wcf1_user WHERE username = '"..account.."'") 
        local result = dbPoll(query,-1) 
        for _,t in ipairs(result) do 
            for k,v in pairs(t) do 
                outputChatBox(tostring(t).." -> index: "..tostring(k).." -> value: "..tostring(v),root) 
            end 
       end 
   end 
end 
addEvent( "CarBuy", true  ) 
addEventHandler( "CarBuy", root, vehicleCreat ) 
  

Use this and post what it returns.

Link to comment
local account = tostring(getElementData ( source, "username" )) 

this should be like

local account = tostring(getElementData ( player, "username" )) 

I think

If source is the player then it should work but there's nothing bad with trying. :)

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