Karuzo Posted March 29, 2014 Share Posted March 29, 2014 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
Weii. Posted March 29, 2014 Share Posted March 29, 2014 check if he has car like if (#datas == 0) then -- You have no car end Link to comment
Karuzo Posted March 29, 2014 Author Share Posted March 29, 2014 Yeah but that's not the only thing i've got in that table so i especially need the hascar column. I made the same thing with my login script and that works great there. Link to comment
BlueBerry Posted March 29, 2014 Share Posted March 29, 2014 Are you entirely sure that result[1] contains data, because it complaints about being unable to index a value. Link to comment
Weii. Posted March 29, 2014 Share Posted March 29, 2014 do outputChatBox(toJSON(datas)) and post here Link to comment
Karuzo Posted March 29, 2014 Author Share Posted March 29, 2014 huh weird it says it expected a string but got nil. So it doesn't get the data right ? How should i solve this ? Link to comment
cheez3d Posted March 29, 2014 Share Posted March 29, 2014 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
Karuzo Posted March 29, 2014 Author Share Posted March 29, 2014 That outputs everything which exists in that table. I don't need to post that, since those datas are passwords etc.. Link to comment
cheez3d Posted March 29, 2014 Share Posted March 29, 2014 Does it return nil anywhere? Also note that your query is vulnerable to injections. Use question marks. local query = dbQuery(connection, "SELECT * FROM wcf1_user WHERE username = ?",account) Link to comment
Weii. Posted March 29, 2014 Share Posted March 29, 2014 local account = tostring(getElementData ( source, "username" )) this should be like local account = tostring(getElementData ( player, "username" )) I think Link to comment
Saml1er Posted March 29, 2014 Share Posted March 29, 2014 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
Karuzo Posted March 29, 2014 Author Share Posted March 29, 2014 local account = tostring(getElementData ( source, "username" )) this should be like local account = tostring(getElementData ( player, "username" )) I think Haha thank you! Works, idk why 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