Jump to content

SetElementData/GetElementData


Recommended Posts

--client
function HudOpening ()
local meatn = getElementData(localPlayer, "meat") or 0
    dxDrawText("Meat: "..math.floor(meatn).." ед", 1800*x, 750*y, 1670*x, 25*y, tocolor(255, 255, 255, 255), 2, "Arial", "center", "center", false, false, true, true, false)
end
addEventHandler("onClientRender",root,HudOpening)

function initGUI( )
    btnOutput = DGS:dgsCreateButton( 0.7, 0.1, 0.2, 0.1, "+", true )
    addEventHandler ( "onDgsMouseClick", btnOutput, outputEditBox )
end
addEventHandler( "onClientResourceStart", getResourceRootElement( getThisResource( ) ), initGUI )

function outputEditBox ( button, state )
    if button == "left" and state == "down" then
    triggerServerEvent( "m9cko", getLocalPlayer(), 0, 24, 35 )
    end
end

--server
function hhh ()
takePlayerMoney(client,2)
setElementData(client, "meat", (getElementData(client, "meat") or 0) + 10)
end
addEvent("m9cko",true)
addEventHandler("m9cko",getRootElement(),hhh)

ElementData = meatn 

When I press the button, it is added +10 (meat) for localplayer

Everything works as it should, but how do I bind to some object or marker, or create a separate variable for this ElementData(not localPlayer)

local meatn = getElementData(localPlayer, "meat") or 0

I mean Instead "localplayer" Create a variable instead or bind to an object. How to do it?

--client

local meatn = getElementData(localPlayer, "meat") or 0
--server
setElementData(client, "meat", (getElementData(client, "meat") or 0) + 10)

I just need instead "localPlayer", "client" create a variable that is not bound to the Localplayer.

Link to comment
  • Moderators

@slapz0r

 

You can use also:

root -- attach to the mother element of all elements

resourceRoot -- attached to the resource which it is currently executed in

local newElement = createElement("elementName") -- attach to a new element of your choice

 

  • Like 1
Link to comment
59 minutes ago, IIYAMA said:

@slapz0r

 

You can use also:


root -- attach to the mother element of all elements

resourceRoot -- attached to the resource which it is currently executed in

local newElement = createElement("elementName") -- attach to a new element of your choice

 

local newElement = createElement("elementName")

local meatn = getElementData(localPlayer, "meat") or 0 -- old
local meatn = getElementData(elementName, "meat") or 0 -- new

--
setElementData(client, "meat", (getElementData(client, "meat") or 0) + 10) -- old with localplayer
setElementData(elementName, "meat", (getElementData(elementName, "meat") or 0) + 10) -- new

And how to use it correctly? 

Link to comment
  • Moderators
42 minutes ago, slapz0r said:

local newElement = createElement("elementName")

local meatn = getElementData(localPlayer, "meat") or 0 -- old
local meatn = getElementData(elementName, "meat") or 0 -- new

--
setElementData(client, "meat", (getElementData(client, "meat") or 0) + 10) -- old with localplayer
setElementData(elementName, "meat", (getElementData(elementName, "meat") or 0) + 10) -- new

And how to use it correctly? 

Use newElement, that is a variable.

not elementName which is just a "string". Or in this case an empty variable.

 

Edited by IIYAMA
  • Like 1
Link to comment

Basically:
 

--Client

local newElement = createMarker(0,0,0,"cylinder",1,255,255,255,255)

if ( newElement ) then
    setElementData(newElement, "meat", (getElementData(newElement, "meat") or 0) + 10)
end


As long as this is client-sided it should work as intended.

  • Like 1
Link to comment
1 hour ago, Shux said:

Basically:
 


--Client

local newElement = createMarker(0,0,0,"cylinder",1,255,255,255,255)

if ( newElement ) then
    setElementData(newElement, "meat", (getElementData(newElement, "meat") or 0) + 10)
end


As long as this is client-sided it should work as intended.

Did as you wrote, and used for display number

dxDrawText("Мясо: "..math.floor(meatnz).." ед", 1800*x, 750*y, 1670*x, 25*y, tocolor(255, 255, 255, 255), 2, "Arial", "center", "center", false, false, true, true, false)
newElement = createPickup (1322.2575683594,358.23822021484,19.5546875, 3, 2663, 1, 1 )

function HudOpening ()
local meatn = getElementData(localPlayer, "meat") or 0
local meatnz = getElementData(newElement, "meat") or 0
    dxDrawText("Мясо: "..math.floor(meatnz).." ед", 1800*x, 750*y, 1670*x, 25*y, tocolor(255, 255, 255, 255), 2, "Arial", "center", "center", false, false, true, true, false)
end
addEventHandler("onClientRender",root,HudOpening)

local player = getLocalPlayer()
function initGUI( )
--showCursor(true)
    btnOutput = DGS:dgsCreateButton( 0.7, 0.1, 0.2, 0.1, "+", true )
    addEventHandler ( "onDgsMouseClick", btnOutput, outputEditBox )
end
addEventHandler( "onClientResourceStart", getResourceRootElement( getThisResource( ) ), initGUI )

function outputEditBox ( button, state)
    if button == "left" and state == "down" then
	if (newElement) then
	setElementData(newElement, "meat", (getElementData(newElement, "meat") or 0) + 10)
    --triggerServerEvent( "m9cko", root,  0, 24, 35 )	
    end
	end
end

Not working, what am I doing wrong or again I do not understand

It works as intended for localplayer and used in server part:

function hhh ()
takePlayerMoney(client,2)
setElementData(client, "meat", (getElementData(client, "meat") or 0) + 10)
end
addEvent("m9cko",true)
addEventHandler("m9cko",root,hhh)

 

7 minutes ago, slapz0r said:

Did as you wrote, and used for display number


dxDrawText("Мясо: "..math.floor(meatnz).." ед", 1800*x, 750*y, 1670*x, 25*y, tocolor(255, 255, 255, 255), 2, "Arial", "center", "center", false, false, true, true, false)

newElement = createPickup (1322.2575683594,358.23822021484,19.5546875, 3, 2663, 1, 1 )

function HudOpening ()
local meatn = getElementData(localPlayer, "meat") or 0
local meatnz = getElementData(newElement, "meat") or 0
    dxDrawText("Мясо: "..math.floor(meatnz).." ед", 1800*x, 750*y, 1670*x, 25*y, tocolor(255, 255, 255, 255), 2, "Arial", "center", "center", false, false, true, true, false)
end
addEventHandler("onClientRender",root,HudOpening)

local player = getLocalPlayer()
function initGUI( )
--showCursor(true)
    btnOutput = DGS:dgsCreateButton( 0.7, 0.1, 0.2, 0.1, "+", true )
    addEventHandler ( "onDgsMouseClick", btnOutput, outputEditBox )
end
addEventHandler( "onClientResourceStart", getResourceRootElement( getThisResource( ) ), initGUI )

function outputEditBox ( button, state)
    if button == "left" and state == "down" then
	if (newElement) then
	setElementData(newElement, "meat", (getElementData(newElement, "meat") or 0) + 10)
    --triggerServerEvent( "m9cko", root,  0, 24, 35 )	
    end
	end
end

Not working, what am I doing wrong or again I do not understand

It works as intended for localplayer and used in server part:


function hhh ()
takePlayerMoney(client,2)
setElementData(client, "meat", (getElementData(client, "meat") or 0) + 10)
end
addEvent("m9cko",true)
addEventHandler("m9cko",root,hhh)

 

UPDATE: SOLVED.

Your example helped me a lot. Did everything as intended.

Thank you all very much for the answers.

Edited by slapz0r
Link to comment

Another question, how to make the numbers saved? On the local player, they are saved after restarting the script, but on "NewElement" not save

How to save these numbers for variable "NewElement" or create xml file and to keep these numbers there, and then read. The main thing to do is save, the method is not important

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...