Jump to content

getElementdata() returning false on client


Yazir

Recommended Posts

Hello! I have a problem. I can't get getElementData() to work locally. It is initiated by server and synced.

-- here's the initiation
	if getElementData(player,"money") == false then
		setElementData(player,"money",100)
	end


-- that's code from another resource
    local money = getElementData( localPlayer, "money")
    outputChatBox( tostring(money) ) -- return: false

And as I said there. I set it at one resource then i want to retrieve it at other. Maybe it's the problem? Yes. I've read the wiki but there's not much on client usage.

Link to comment
-- here's the initiation
	setElementData(player,"money",100)


-- that's code from another resource
    local money = getElementData( localPlayer, "money")
    if money then
    	outputChatBox( tostring(money) )
    end

Try that^. When you don't have the element data "money" set it is not 'false', it's just not there so no need to check I think.

Edited by koragg
Link to comment

If you want to make such thing on client side use tables and export them among the scripts because it's very useful and safely to use them if you don't need to synchronize this data with all scripts.

usermoney = {} -- Create empty table to insert data

Setting data for player example(an element is used as table's index in MTA):

usermoney[player] = 500 -- Set player's money to 500

 

Edited by NeverUnbeatable
Link to comment
9 hours ago, koragg said:

When you don't have the element data "money" set it is not 'false', it's just not there so no need to check I think.

Returns

This function returns a variable containing the requested element data, or false if the element or the element data does not exist. When getting data corresponding to a XML attribute, this is always a string.

When the data is not found, it DOES return false.

 

@Yazir your script seems fine. So:

  1. Are you sure the init script is called before the client?
  2. Are you sure the "player" parameter is correct? Maybe it's for some reason invalid, and the data is not set. Also an error is thrown
  3. Did you copied-pasted the scripts? (just to make sure there are no syntax errors in the script)
  4. From point 2, are there errors/warnings thrown in general? Check both clients and server

 

As is said the script seems fine to me, setElementData by default sync the data server<->client, and it depends on elements, shared between resources (so fetching the data from different resource is not the problem as you said). Try also adding (if you haven't done it yet) output inside the code blocks, so you "see" what's happening inside your scripts (this is just to check the "logic" part of the script in case point 4 is "there are no errors": in case the server block for some reason is skipped, the data will be false)

That's all i can think right now

  • Like 2
Link to comment

I know what is the reason. I thought the ElementData stays on players even when they disconnect. It doesn't so I have another question. How can i save that data so it could be retrieved when the player connects? If you have any quick answers, please do. In the meantime I'll be looking around in some already done resources to see how it has been made.

To answer your @LoPollo though:

1. Yes

2. Yes.

3. Yes, fragments.

4. There have been errors that this value is a boolean, but it is known why now.

Edited by Yazir
Link to comment

You can save data in lots of ways:

if the data is relevant only for the current session of the server/resource, you can save the data at root/resourceRoot (this data will be lost on server restart/resource restart)

You can also save data with accounts. The player must be logged in, as guests accounts will have their data deleted on disconnect

You can use databases, using serials to identify the players...

 

and many others

 

The best way is using accounts in my opinion

Edited by LoPollo
  • Like 1
Link to comment

I've tried using accounts but client can't read it (i guess that's good) so I think I'll have to load it when player connects and save when he disconnects.

If server is shutdown, does "onPlayerQuit" fire for every player on the server?

 

 

Or I could save it when the money changes.

Edited by Yazir
Link to comment
8 minutes ago, Yazir said:

Maybe I didn't make it clear. When there are players on the server and I type shutdown in the console. Will onPlayerQuit still get triggered?

shutdown is not a command (if i didn't forget anything).. do you mean quit? when a client types quit, yes the server onPlayerQuit gets triggered. If you mean that you have a script that add the command shutdown to call the function Shutdown... then it's a good question, i don't know the answer honestly. But you can find it out testing

 

PS: if it's a source of confusion, server events exist only for server-side scripts, and client events exist only for client-side script.

EDIT: 

3 minutes ago, Yazir said:

Will event "onPlayerQuit" be activated when server gets shut down (not when it crashes, of course it won't then).

 

That's the third time I ask this question and I'll probably check it myself.

PS: in this case i'm sure onResourceStop gets called, just so you know

Edited by LoPollo
Link to comment

@LoPollo this is other question that you might know the answer. Instead of spamming I'll write it here.

Why when I set scoreboard to this 

local players = getElementsByType( "player" )

function setScoreboard( )
	call ( getResourceFromName( "scoreboard" ), "scoreboardResetColumns")
	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "test", 70, "tetsefeas",17)

	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "murders", 70, "Player kills", 12)
	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "zombieskilled", 70, "Zombie kills",13)
	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "headshots", 70, "Headshots",15)
	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "daysalive", 70, "Days Alive",16)
	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "totalkills", 70, "Total Frags",14)
	call ( getResourceFromName( "scoreboard"), "scoreboardSetColumnPriority", "name", 11)
	for theKey,thePlayer in ipairs(players) do
		setElementData( thePlayer, "test", 20 )

		outputChatBox( "3")
	end
end

addEventHandler( "onClientResourceStart", getRootElement(  ), setScoreboard )

It shows me this in game:

2ed1c8a160c115ed96bd99da317bbddd.png

 

It worked before. The next day it went to this.

 

 

 

 

@edit 

Oh I know what I'm doing wrong here. Let me check if it is the reason.

 

@edit2

It won't work but I've updated the code anyway.

Edited by Yazir
Link to comment

I'm not an expert on scoreboards, but after reading Resource:Dxscoreboard i think i know why it's happening:

 

Quote

bool scoreboardAddColumn ( string name, [ element forElement = getRootElement(), int width = 70, string friendlyName = name, int priority = slot after "name" column ] )

^^^that's the syntax

what's below are the parameters you pass.

"test", 70, "tetsefeas",17

test is the name, 70 i think it should be the width BUT is the forElement, and since it's not an element the function scoreboardAddColumn fails.

The fail may cause the problem.

 

See if this works (i just inserted root as forElement):

local players = getElementsByType( "player" )

function setScoreboard( )
	call ( getResourceFromName( "scoreboard" ), "scoreboardResetColumns")
	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "test", 70, "tetsefeas",17)
	
	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "murders", 70, "Player kills", 12)
	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "zombieskilled", 70, "Zombie kills",13)
	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "headshots", 70, "Headshots",15)
	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "daysalive", 70, "Days Alive",16)
	call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "totalkills", 70, "Total Frags",14)
	call ( getResourceFromName( "scoreboard"), "scoreboardSetColumnPriority", "name", 11)
	for theKey,thePlayer in ipairs(players) do
		setElementData( thePlayer, "test", 20 )
		outputChatBox( "3")
	end
end

addEventHandler( "onClientResourceStart", getRootElement(  ), setScoreboard )

 

13 hours ago, Yazir said:

Instead of spamming I'll write it here.

Instead it's a good idea to create another post, it's another argument. Using this general rule, everyone will be able to find what's his looking for faster, if having a similar problem.

Edited by LoPollo
Link to comment

Didn't notice that, sorry.

Are there errors clientside?

Try also do some basic debug by adding some output between lines, checking also the returns of the calls.

Try also temporarily commenting the call to the function  scoreboardSetColumnPriority on name:

int priority = slot after "name" column 
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...