Lawliet Posted June 10, 2015 Share Posted June 10, 2015 Before I explain my problem, allow me to describe a scenario. Let us assume you have database called database.db, which stores all info about players (such as name, health, money, position, etc). Now, you wish to retrieve some of the data from the local player and display it via some GUI, let's say, via dxDrawImage. The account is retrieved by using getAccountName. The data you wish to retrieve is, in this scenario, health, hunger and thirst. Said data will be retrieved by using triggerServerEvent from the client. Naturally, on the serverside, you would do this: -- [[ SERVER SIDE ]] -- c_database = dbConnect("sqlite","database/database.db") -- Using the queried data from queryDataFromDB, we assign each value to a variable (health, hunger & thirst) function insertDataFromTable() passed_data = dbPoll(data_table,0) for i,thedata in ipairs(passed_data) do health = thedata.health hunger = thedata.hunger thirst = thedata.thirst end end -- We query for health, hunger & thirst for the player who called the event, who happens to the local player function queryDataFromDB() account = getAccountName(getPlayerAccount(client)) data_table = dbQuery(c_database,"SELECT health,hunger,thirst FROM player_stats WHERE account=?",account) setTimer(insertDataFromTable,1000,1) end addEvent("onQueryDataFromDB",true) addEventHandler("onQueryDataFromDB",root,queryDataFromDB) Do you begin to see the problem here? We have assigned health, hunger and thirst. Let us assume the values are 100, 50 and 20. Now, how do you pass them to the local player? I've been racking my brain for hours over this particular problem now. It basically amounts to this: I'm perfectly able to pass all data as long as it serversided, but as soon as I try to pass data from the server to the client...maybe I'm not seeing the obvious solution here, but in any case, I'd be most grateful if someone could help me out with this. Link to comment
xXMADEXx Posted June 10, 2015 Share Posted June 10, 2015 Are you talking about triggerClientEvent? Link to comment
Lawliet Posted June 10, 2015 Author Share Posted June 10, 2015 I tried using triggerClientEvent, but to no avail. Returns false, probably because it doesn't pass the data to the client. This begs, naturally, the question whether or not I have done it correctly. I don't mind if someone else was to post his way of passing the data to the local player. I assume triggerClientEvent is the only efficient way to do it, but how exactly do I use it in conjunction with the retrieved data? Oh, and don't even think about directing me to the wiki page, because I stared at that for hours. I know that you can pass arguments using triggerClientEvent, but...how do I do that, and how to use said passed data on the client side? Link to comment
Walid Posted June 10, 2015 Share Posted June 10, 2015 I tried using triggerClientEvent, but to no avail. Returns false, probably because it doesn't pass the data to the client. This begs, naturally, the question whether or not I have done it correctly. I don't mind if someone else was to post his way of passing the data to the local player. I assume triggerClientEvent is the only efficient way to do it, but how exactly do I use it in conjunction with the retrieved data? Oh, and don't even think about directing me to the wiki page, because I stared at that for hours. I know that you can pass arguments using triggerClientEvent, but...how do I do that, and how to use said passed data on the client side? There's more than one way to do it example: -- Server side data_table = dbQuery(c_database,"SELECT health,hunger,thirst FROM player_stats WHERE account=?",account) triggerClientEvent(client,"EventName",client,data_table) -- Client Side function YourFunction(table) for i , v in pairs (table) do -- bla bla end end Link to comment
Lawliet Posted June 10, 2015 Author Share Posted June 10, 2015 There's more than one way to do it example: -- Server side data_table = dbQuery(c_database,"SELECT health,hunger,thirst FROM player_stats WHERE account=?",account) triggerClientEvent(client,"EventName",client,data_table) -- Client Side function YourFunction(table) for i , v in pairs (table) do -- bla bla end end Tried that, didn't work. At first, I got "expected table, got nil" from the clientside, figured it was because the query wasn't in dbPoll, so I adjusted the code here and there. It's still not working, though. The main problem here is that you can't put dbPoll and dbQuery into the same function, because it always returns nil (meaning the Query isn't finished yet by the time you do dbPoll), so you have to put both of them in two different functions. So yeah, if you adjust your code with dbPoll in mind, I'd appreciate it a lot, Walid. So far, it does sound good. Link to comment
bradio10 Posted June 11, 2015 Share Posted June 11, 2015 Have you tried passing the data to the clientside after you've done the loop on the server? Something like: -- [[ SERVER SIDE ]] -- c_database = dbConnect("sqlite","database/database.db") -- Using the queried data from queryDataFromDB, we assign each value to a variable (health, hunger & thirst) function insertDataFromTable() passed_data = dbPoll(data_table,0) for i,thedata in ipairs(passed_data) do health = thedata.health hunger = thedata.hunger thirst = thedata.thirst end triggerClientEvent(client, "eventName", client, health, hunger, thirst) end -- We query for health, hunger & thirst for the player who called the event, who happens to the local player function queryDataFromDB() account = getAccountName(getPlayerAccount(client)) data_table = dbQuery(c_database,"SELECT health,hunger,thirst FROM player_stats WHERE account=?",account) setTimer(insertDataFromTable,1000,1) end addEvent("onQueryDataFromDB",true) addEventHandler("onQueryDataFromDB",root,queryDataFromDB) I don't think I've used client for the triggerClientEvent properly, but do you get what I mean? EDIT: I just tested it and this works -- [[ SERVER SIDE ]] -- c_database = dbConnect("mysql", "dbname=DATABASE;host=localhost;port=3306", "USERNAME", "PASSWORD" ) local tbl = dbExec(c_database, "CREATE TABLE IF NOT EXISTS player_stats (account TEXT, health INT, hunger INT, thirst INT)") -- Using the queried data from queryDataFromDB, we assign each value to a variable (health, hunger & thirst) function insertDataFromTable(plr) passed_data = dbPoll(data_table,0) for i,thedata in ipairs(passed_data) do health = thedata.health hunger = thedata.hunger thirst = thedata.thirst end triggerClientEvent(plr, "sendToClient", plr, health, hunger, thirst) end -- We query for health, hunger & thirst for the player who called the event, who happens to the local player function queryDataFromDB() account = getAccountName(getPlayerAccount(client)) data_table = dbQuery(c_database,"SELECT health,hunger,thirst FROM player_stats WHERE account=?",account) setTimer(insertDataFromTable,1000,1, client) end addEvent("onQueryDataFromDB",true) addEventHandler("onQueryDataFromDB",root,queryDataFromDB) Clientside: addCommandHandler("getdata", function() triggerServerEvent("onQueryDataFromDB", localPlayer) outputChatBox("Asked for data", 255, 255, 0) end ) addEvent("sendToClient", true) addEventHandler("sendToClient", root, function(health, hunger, thirst) if (health and hunger and thirst) then outputChatBox("Health: " .. tostring(health) .. ", Hunger: " .. tostring(hunger) .. ", Thirst: " .. tostring(thirst), 255, 255, 0) else outputChatBox("Not there", 255, 255, 0) end end ) Once I did the command, I received the output. Link to comment
Walid Posted June 11, 2015 Share Posted June 11, 2015 Tried that, didn't work. At first, I got "expected table, got nil" from the clientside, figured it was because the query wasn't in dbPoll, so I adjusted the code here and there. It's still not working, though. The main problem here is that you can't put dbPoll and dbQuery into the same function, because it always returns nil (meaning the Query isn't finished yet by the time you do dbPoll), so you have to put both of them in two different functions. So yeah, if you adjust your code with dbPoll in mind, I'd appreciate it a lot, Walid. So far, it does sound good. It's easy all what you need is -- Server side function getPlayerStats(player) local data = dbPoll(dbQuery(db, "SELECT * FROM player_stats WHERE account = ?", getAccountName(getPlayerAccount(player))), -1) if type(data) ~= "table" or #data ~= 0 or data then return data else return false end end function queryDataFromDB() local account = getPlayerAccount(client) if account and not isGuestAccount(account) then local Data = getPlayerStats(client) triggerClientEvent(client, "sendToClient", client,Data) end end addEvent("onQueryDataFromDB",true) addEventHandler("onQueryDataFromDB",root,queryDataFromDB) -- Client side function YourFunction(table) for i , v in pairs (table) do -- bla bla end end addEvent("sendToClient",true) addEventHandler("sendToClient",root,YourFunction) Link to comment
Lawliet Posted June 11, 2015 Author Share Posted June 11, 2015 Have you tried passing the data to the clientside after you've done the loop on the server? [...] Thanks, bradio10, your suggestion was the one I was searching for. Had to adjust it a bit, but now it works like a charm. And Walid, thanks for your help, too. Maybe I can put your code to some other use. Link to comment
novo Posted June 11, 2015 Share Posted June 11, 2015 Aware that this is already solved, just came to recommend you using latentEvents when transfering a large data amount. 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