Jump to content

[help] Object save


XLEGENDX

Recommended Posts

hello guys, i was trying to make it save object when i reconnect can anyone help me?

[[ Client ]]

function finel ()

  local item = guiGridListGetSelectedItem (myGirld)
    ID = guiGridListGetItemText (myGirld, item,3)
    body = guiGridListGetItemText (myGirld, item,4)
    rot = guiGridListGetItemText (myGirld, item,5)
    prices = guiGridListGetItemText (myGirld, item,2)
    triggerServerEvent( "give",localPlayer,prices,ID,body,rot) 
    showCursor(false)
    destroyElement(window)
    
end
addEventHandler("onClientGUIClick", buying, finel)

[[ Server ]]

local obj = {}

function maske_kaldir2(pay,id,bdy,rot)
local money = getPlayerMoney(client)                            
    if (money > tonumber (pay)) then     
        takePlayerMoney (client,(pay))
        obje[source] = createObject ( objeid, 0, 0, 0, 0, 0, 0 )
        exports.bone_attach:attachElementToBone(obje[source],source,1,0,0,-0.61,0,0,90)
    else
        outputChatBox ("You dont have Money" ,client ,255,0,0)
    end
end
addEvent("give",true) 
addEventHandler("give", root, maske_kaldir2)

 

Edited by XLEGENDX
Link to comment

Your code is a bit messy, I fixed a few typos that you had. Not gonna suggest SQL, it may be a bit more complex for you so for now just use accountData to save the ID. Player has to be logged in for this to work. Also added an extra function for when the player logs in, if the data was saved to their account it'll respawn on login.

[[ Client ]]

function finel ()

  local item = guiGridListGetSelectedItem (myGirld)
    ID = guiGridListGetItemText (myGirld, item,3)
    body = guiGridListGetItemText (myGirld, item,4)
    rot = guiGridListGetItemText (myGirld, item,5)
    prices = guiGridListGetItemText (myGirld, item,2)
    triggerServerEvent( "give",localPlayer, localPlayer, prices,ID,body,rot) 
    showCursor(false)
    destroyElement(window)
    
end
addEventHandler("onClientGUIClick", buying, finel)

[[ Server ]]

local obj = {}

function maske_kaldir2(plr,pay,id,bdy,rot)
local money = getPlayerMoney(plr)                            
    if (money > tonumber (pay)) then     
        local account = getPlayerAccount(plr)
		if (not account) then return end
		setAccountData(account, "objectID", id)
		takePlayerMoney (plr,(pay))
        obj[plr] = createObject ( id, 0, 0, 0, 0, 0, 0 )
        exports.bone_attach:attachElementToBone(obj[plr],plr,1,0,0,-0.61,0,0,90)
    else
        outputChatBox ("You dont have Money" ,plr ,255,0,0)
    end
end
addEvent("give",true) 
addEventHandler("give", root, maske_kaldir2)

addEventHandler("onPlayerLogin", root,
	function()
		if (isElement(source) and getElementType(source) == "player") then
			if (getPlayerAccount(source)) then
				local account = getPlayerAccount(source)
				local object = getAccountData(account, "objectID")
				if (object) then
					obj[source] = createObject ( object, 0, 0, 0, 0, 0, 0 )
					exports.bone_attach:attachElementToBone(obj[source],source,1,0,0,-0.61,0,0,90)
					outputChatBox("Object given", source, 255, 0, 0)
				end
			end
		end
	end
)

 

  • Thanks 1
Link to comment
  • Moderators

You can store a table, if you convert it to a string before save.

 

https://wiki.multitheftauto.com/wiki/ToJSON

https://wiki.multitheftauto.com/wiki/FromJSON

 

local table = { "dogs", cat = "hungry", mouse = "food", birds = 4 }
local table_string = toJSON(table) -- result: [ { "1": "dogs", "mouse": "food", "cat": "hungry", "birds": 4 } ]
local table_again_from_string = fromJSON(table_string) -- table == table_again_from_string

 

Edited by stPatrick
Link to comment
17 hours ago, XLEGENDX said:

@Ab-47

 

Thank you for your answer !

  Here, I also wanted to save multiple objects. Accordingly, I had to save multiple IDs on accountData. As I know, setAccountData can't store tables on account. If I want to save IDs 11, 12 and 13, what should I do here ?

Sorry for the late reply, in simple terms you can just call the setAccountData function again and again for as many variables as you like but the name must change

setAccountData(account, "objectID1", id1)

setAccountData(account, "objectID2", id2)

setAccountData(account, "objectID3", id3)

But to retrieve it you must call each piece of data, objectID1, objectID2, objectID3 and so on. But that's all time consuming and pointless

A better way to do that is by setting the toJSON converted table to the account data and use fromJSON to retrieve it as stPatrick mentioned above.

8 hours ago, stPatrick said:

You can store a table, if you convert it to a string before save.

 

https://wiki.multitheftauto.com/wiki/ToJSON

https://wiki.multitheftauto.com/wiki/FromJSON

 


local table = { "dogs", cat = "hungry", mouse = "food", birds = 4 }
local table_string = toJSON(table) -- result: [ { "1": "dogs", "mouse": "food", "cat": "hungry", "birds": 4 } ]
local table_again_from_string = fromJSON(table_string) -- table == table_again_from_string

 

If you need any additional help, do let us know.

Edited by Ab-47
  • Thanks 1
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...