thund3rbird23 Posted August 11, 2020 Share Posted August 11, 2020 How can I send these values from server-side to a client-sided list? function weaponmodels() for _, player in ipairs(getElementsByType("player")) do if getElementData(player, "loggedin") then local owner = getElementData(player, "acc:id") local query = dbQuery(con,"SELECT * FROM weapons WHERE owner = ?", owner) local weapons = dbPoll(query,-1) if weapons then for k,v in ipairs(weapons) do weapid = v["id"] weapmodel = v["model"] end end end end end I want to send the weapid and weapmodel to a client-sided list of course this is not working : local items = { {weapid,weapmodel, 1}, } Link to comment
MTA Team 0xCiBeR Posted August 12, 2020 MTA Team Share Posted August 12, 2020 You need to use: https://wiki.multitheftauto.com/wiki/TriggerClientEvent Or use something that is synced, like elementData. Link to comment
thund3rbird23 Posted August 12, 2020 Author Share Posted August 12, 2020 (edited) I did it with setElementData and working but this is showing only the last data from mysql... I want to display every data... for ex.: if you have 5 weapons then show 5 weapons not only the latest one. How can I do that? If I print the "weapmodel" to the chatbox like "outputChatBox(weapmodel)" after the mysql query string in server-side then showing all of my weapons... --- server side --- addEvent("weaponmodels",true) addEventHandler("weaponmodels",getRootElement(),function(player) for _, player in ipairs(getElementsByType("player")) do if getElementData(player, "loggedin") then local owner = getElementData(player, "acc:id") local query = dbQuery(con,"SELECT * FROM weapons WHERE owner = ?", owner) local weapons = dbPoll(query,-1) if weapons then for k,v in ipairs(weapons) do weapmodel = v["model"] setElementData(source, weapmodel, true) end end end end end) --- client side --- local weapon = getElementData(root, "weapmodel") local items = { {weapon}, } function draw() for i = currentCell + 1, maxRows + currentCell do local value = items[i] local key = i - currentCell - 1 if value ~= nil then dxDrawText(value[1], screenW * 0.759, screenH * 0.3269+(key*54), screenW * 0.2380, screenH * 0.3519, tocolor(255, 255, 255, 255), 1.00, "default-bold", "left", "center", false, false, false, false, false) end end end addEventHandler("onClientRender", root, draw) Edited August 12, 2020 by thund3rbird23 Link to comment
Moderators IIYAMA Posted August 12, 2020 Moderators Share Posted August 12, 2020 (edited) 56 minutes ago, thund3rbird23 said: If I print the "weapmodel" to the chatbox like You are SET the elementData to each player element and you try to GET the elementdata from the root element. (Not the same element) Recommendation: - SET and GET it with the resourceRoot. - GET the data each frame, because you do not know when it is available. Edited August 12, 2020 by IIYAMA Link to comment
thund3rbird23 Posted August 12, 2020 Author Share Posted August 12, 2020 (edited) 37 minutes ago, IIYAMA said: You are SET the elementData to each player element and you try to GET the elementdata from the root element. (Not the same element) Recommendation: - SET and GET it with the resourceRoot. - GET the data each frame, because you do not know when it is available. Thanks, I replaced both to resourceRoot, now I get false in debug. --- set the elementdata in server-side --- setElementData(resourceRoot, "weaponmodel", weapmodel) --- get the elementdata in client-side & debug --- local model = getElementData ( resourceRoot, "weaponmodel" ) outputDebugString(model) EDIT: if I set the element to player in server-side and localPlayer in client-side then I still got only one weaponmodel (in client-side) instead of the 5 weaponmodel which I have in the database. Edited August 12, 2020 by thund3rbird23 Link to comment
Moderators IIYAMA Posted August 12, 2020 Moderators Share Posted August 12, 2020 (edited) 33 minutes ago, thund3rbird23 said: if I set the element to player in server-side and localPlayer in client-side then I still got only one weaponmodel (in client-side) instead of the 5 weaponmodel which I have in the database. Don't save it per player. Because everybody will download the data double/triple/10x/100x/1000x... Save all data inside of the element resourceRoot, under the key "weaponmodel" local weapons = dbPoll(query,-1) if weapons then setElementData(resourceRoot, "weaponmodel", weapons) end Now debug the data, when it is available. local weapons function draw() weapons = weapons or getElementData(resourceRoot, "weaponmodel") -- if no data from buffer, get the data. if weapons then dxDrawText(inspect(weapons), 300, 300) end end addEventHandler("onClientRender", root, draw) Edited August 12, 2020 by IIYAMA 1 Link to comment
thund3rbird23 Posted August 12, 2020 Author Share Posted August 12, 2020 4 minutes ago, IIYAMA said: Don't save it per player. Because everybody will download the data double/triple/10x/100x/1000x... Save all data inside of the element resourceRoot, under the key "weaponmodel" local weapons = dbPoll(query,-1) if weapons then setElementData(resourceRoot, "weaponmodel", weapons) end Now debug the data, when it is available. function draw() local weapons = getElementData(resourceRoot, "weaponmodel") if weapons then dxDrawText(inspect(weapons), 300, 300) end end addEventHandler("onClientRender", root, draw) Okay now it's displays more information... but how can I display only the "model" row? I know that I need to change the mysql query to: local query = dbQuery(con,"SELECT model FROM weapons WHERE owner = ?", owner) local weapons = dbPoll(query,-1) if weapons then setElementData(resourceRoot, "weaponmodel", weapons) end But in this way I got a table with model string on the screen: {{ model = 123 }, { model = 231 }, { model = 312 }, { model = 132 }, { model = 321 }} I want to display only the numbers. This is showing only the numbers but only one model not the 5 models what I have. local query = dbQuery(con,"SELECT * FROM weapons WHERE owner = ?", owner) local weapons = dbPoll(query,-1) if weapons then for k,v in ipairs(weapons) do weapmodel = v["model"] setElementData(resourceRoot, "weaponmodel", weapmodel) end end Link to comment
Moderators IIYAMA Posted August 12, 2020 Moderators Share Posted August 12, 2020 1 minute ago, thund3rbird23 said: Okay now it's displays more information... but how can I display only the "model" row? By putting it in a new table. local newTable = {} for k,v in ipairs(weapons) do newTable[#newTable + 1] = v["model"] end and SET that. 1 Link to comment
thund3rbird23 Posted August 12, 2020 Author Share Posted August 12, 2020 21 minutes ago, IIYAMA said: By putting it in a new table. local newTable = {} for k,v in ipairs(weapons) do newTable[#newTable + 1] = v["model"] end and SET that. local query = dbQuery(con,"SELECT * FROM weapons WHERE owner = ?", owner) local weapons = dbPoll(query,-1) if weapons then for k,v in ipairs(weapons) do newTable[#newTable + 1] = v["model"] setElementData(resourceRoot, "weaponmodel", newTable) end end Output doesn't seems like a table: {{ 123, 321, 231, 312, 132 }} That's what it should look like: {{123}, {321}, {231}, {312}, {132}} Link to comment
Moderators IIYAMA Posted August 12, 2020 Moderators Share Posted August 12, 2020 1 minute ago, thund3rbird23 said: That's what it should look like: Nope, this is what it should become: {123, 321, 231, 312, 132} Unless you want to add more data PER weapon model. Btw if you want to send + get data to each individual: Link to comment
thund3rbird23 Posted August 12, 2020 Author Share Posted August 12, 2020 7 minutes ago, IIYAMA said: Nope, this is what it should become: {123, 321, 231, 312, 132} Unless you want to add more data PER weapon model. Well , my bad then. Thanks for your help! 1 Link to comment
Moderators IIYAMA Posted August 12, 2020 Moderators Share Posted August 12, 2020 38 minutes ago, thund3rbird23 said: Well , my bad then. Thanks for your help! Before I forget. If you want to SET it per player. setElementData(player, "weaponmodel", --[[...]]) GET for yourself. getElementData(localPlayer, "weaponmodel") 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