aka Blue Posted April 23, 2019 Share Posted April 23, 2019 So I have a Lua script which works in both sides (client-server). I use the server-side to store positions in a database. The problem I have is that it does not convert the string into a table in client-side so I can't use the positions I have stored. Exemple: CLIENT-SIDE local sx, sy = guiGetScreenSize() local x, y = ( sx / 1024 ), ( sy / 768 ) local table = { {x*200, y*2,x*50,y*200}, {x*200, y*2,x*50,y*200}, {x*200, y*2,x*50,y*200}, {x*200, y*2,x*50,y*200}, } triggerServerEvent( "storeData", localPlayer, toJSON( table ) ) SERVER-SIDE local data = fromJSON( storedData ) triggerClientEvent( x, "sadas", x, data ) -- It returns nil Is there any way to transform a table into a string and backwards? Thanks. Link to comment
Investor Posted April 24, 2019 Share Posted April 24, 2019 toJSON and fromJSON should work perfectly fine as long as you don't put elements and non-element userdata, which it seems you don't do. What do you mean by "it returns nil"? The triggering of the event returns nil? or it delivers a nil on the client side? Perhaps try sending it over as JSON still and fromJSON it on the client side. Link to comment
aka Blue Posted April 24, 2019 Author Share Posted April 24, 2019 I use toJSON to encode the table I got client-side. The values are dx positions, like sx*222, sy*22, etc. When I want to load the data again with fromJSON I get nil value. I don't know why. I use JSON for a couple of things and I never get this error. Link to comment
Moderators IIYAMA Posted April 24, 2019 Moderators Share Posted April 24, 2019 2 hours ago, aka Blue said: I use toJSON to encode the table I got client-side. The values are dx positions, like sx*222, sy*22, etc. When I want to load the data again with fromJSON I get nil value. I don't know why. I use JSON for a couple of things and I never get this error. A Is the whole table a nil value B or is it a value after indexing the table? In case of A, it is very much possible to get those issues. JavaScript (Object Notation) does not support the data type tables after all. Use iprint to figure out which type of JavaScript objects MTA has assigned for your table format. [ "test"] = array {0:"test" } = object In case of B. First of all, it is better to send over tables instead of JSON. This makes the packet size much smaller and it might also solve your problem for some unknown reason. (Strings much bigger data type than numbers.) Did you set up your database correct that the column supports enough characters? (this can break JSON of course) Debug JSON, show me your JSON! 1 Link to comment
aka Blue Posted April 24, 2019 Author Share Posted April 24, 2019 @IIYAMA I trigger the client-side table formatted with JSON: The table has this default values When I change something, I charge the data from the database (I know it's not the best option but it's for testing) If I don't use fromJSON, I get this in iprint But if I use fromJSON, I get this I don't know where is the problem. I need to trigger the positions to client-side and change the value of posHud table with the database values. If I trigger the result without using fromJSON, I get this I think its not formatting good or something. Thanks for answering! 1 Link to comment
Moderators IIYAMA Posted April 24, 2019 Moderators Share Posted April 24, 2019 3 minutes ago, aka Blue said: If I don't use fromJSON, I get this in iprint Can you show me the complete JSON string? Because this one looks incomplete. This looks fine: [ <-- MTA wrapper [ [ <-- first array (table) [ [ [ <-- sub array (table) This looks like it is cut off, after getting it from the database (on the end of the string) ], [" Not sure if there is a character limit for the console. A quick validation: debugJSON_A = stringA -- JSON from clientside -- get the string from the database: debugJSON_B = stringB if debugJSON_A != debugJSON_B then outputChatBox("database column `hud` is not correct set-up") end 1 Link to comment
aka Blue Posted April 24, 2019 Author Share Posted April 24, 2019 (edited) @IIYAMA Think is that the problem. Its storing wrong the data: Pic from the database. Ok, I used varchar(100). My mistake. Sorry and thanks for the help! Edited April 24, 2019 by aka Blue Link to comment
Moderators IIYAMA Posted April 24, 2019 Moderators Share Posted April 24, 2019 2 minutes ago, aka Blue said: @IIYAMA Think is that the problem. Its storing wrong the data: Pic from the database. It looks like the database column `pos` isn't correctly set-up. A tool you can use, to view this information, make sure the resource isn't running when you use it: https://github.com/sqlitebrowser/sqlitebrowser Link to comment
aka Blue Posted April 24, 2019 Author Share Posted April 24, 2019 Just now, IIYAMA said: It looks like the database column `pos` isn't correctly set-up. A tool you can use, to view this information, make sure the resource isn't running when you use it: https://github.com/sqlitebrowser/sqlitebrowser It was my mistake. I used varchar(100) and it was not storing all the data . Thank you for the help! 1 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