Jump to content

setElementData and getElementData problem


Recommended Posts

I've tried(my first try) to setElementData for new players and regular players(who at least joined once)

i don't know how to do this exactly. how would i check if serial of player has got element data ,etc.. its confusing me.

i have 2 kind of codes(same script file), both return errors.

1st

function setPlayerState(thePlayer)
local elementdata = getElementData(thePlayer)
  if (elementdata == "data.joindataold") then
return
else
setElementData(thePlayer, "data.joindatanew")
end
end
addEventHandler("onPlayerJoin",root,setPlayerState)


function checkPlayerState(thePlayer)
local check = getPlayerSerial(player)
local playername = getPlayerName(thePlayer)
    if getElementData(thePlayer, "data.joindataold") then
      outputChatBox(playername.. "has joined the game [Regular Player]",0,190,90)
      elseif getElementData(thePlayer, "data.joindatanew") then
      outputChatBox(playername.. "has joined the game [New Player]",0,190,90)
end
end
addEventHandler("onPlayerJoin",root,checkPlayerState)

function savePlayerData(thePlayer)
setElementData(thePlayer,check)
end
addEventHandler("onPlayerJoin",root,savePlayerData)

errors:

[15:43:49] WARNING: customjq\script.lua:2: Bad argument @ 'getElementData' [Expected element at argument 1, got nil]

[15:43:49] WARNING: customjq\script.lua:6: Bad argument @ 'setElementData' [Expected element at argument 1, got nil]

[15:43:49] WARNING: customjq\script.lua:13: Bad argument @ 'getPlayerSerial' [Expected player at argument 1, got nil]

[15:43:49] WARNING: customjq\script.lua:14: Bad argument @ 'getPlayerName' [Expected element at argument 1, got nil]

[15:43:49] WARNING: customjq\script.lua:15: Bad argument @ 'getElementData' [Expected element at argument 1, got nil]

[15:43:49] WARNING: customjq\script.lua:17: Bad argument @ 'getElementData' [Expected element at argument 1, got nil]

[15:43:49] WARNING: customjq\script.lua:24: Bad argument @ 'setElementData' [Expected element at argument 1, got nil]

2nd code(same script file)

function setPlayerState()
local elementdata = getElementData(source)
  if (elementdata == "data.joindataold") then
return
else
setElementData(source, "data.joindatanew")
end
end
addEventHandler("onPlayerJoin",root,setPlayerState)


function checkPlayerState()
local check = getPlayerSerial(source)
local playername = getPlayerName(source)
    if getElementData(source, "data.joindataold") then
      outputChatBox(playername.. "has joined the game [Regular Player]",0,190,90)
      elseif getElementData(source, "data.joindatanew") then
      outputChatBox(playername.. "has joined the game [New Player]",0,190,90)
end
end
addEventHandler("onPlayerJoin",root,checkPlayerState)

function savePlayerData()
setElementData(source,check)
end
addEventHandler("onPlayerJoin",root,savePlayerData)

errors:

[15:37:07] WARNING: customjq\script.lua:2: Bad argument @ 'getElementData' [Expected string at argument 2, got nil]

[15:37:07] WARNING: customjq\script.lua:6: Bad argument @ 'setElementData' [Expected argument at argument 3, got none]

[15:37:07] WARNING: customjq\script.lua:24: Bad argument @ 'setElementData' [Expected string at argument 2, got nil]

please help me, i dont understand how to fix/make this as im new to this

btw why dont


tags work?

Link to comment

Have you tried reading the wiki?

https://wiki.multitheftauto.com/wiki/GetElementData

There are some examples there. When using getElementData you need to pass the element you want data off and a key, which is a string. When using setElementData you need pass a third argument which has the value of the data you want to assign to the key.

  
function setPlayerState(thePlayer) 
     local elementdata = getElementData(thePlayer,"data.joindataold") 
     if (elementdata) then 
          return 
     else 
          setElementData(thePlayer, "data.joindatanew", "actual data you want to input") 
     end 
end 
addEventHandler("onPlayerJoin",root,setPlayerState) 
  
  
function checkPlayerState(thePlayer) 
     local check = getPlayerSerial(player) 
     local playername = getPlayerName(thePlayer) 
     if getElementData(thePlayer, "data.joindataold") then 
          outputChatBox(playername.. "has joined the game [Regular Player]",0,190,90) 
     elseif getElementData(thePlayer, "data.joindatanew") then 
          outputChatBox(playername.. "has joined the game [New Player]",0,190,90) 
     end 
end 
addEventHandler("onPlayerJoin",root,checkPlayerState) 
  
function savePlayerData(thePlayer) 
     setElementData(thePlayer,"Data_key","value of data") 
end 
addEventHandler("onPlayerJoin",root,savePlayerData) 
  

 

Try using:

 [/code ]
Link to comment
[18:21:39] WARNING: customjq\script.lua:2: Bad argument @ 'getElementData' [Expected element at argu

ment 1, got nil]

[18:21:39] WARNING: customjq\script.lua:6: Bad argument @ 'setElementData' [Expected element at argu

ment 1, got nil]

[18:21:39] WARNING: customjq\script.lua:13: Bad argument @ 'getPlayerSerial' [Expected player at arg

ument 1, got nil]

[18:21:39] WARNING: customjq\script.lua:14: Bad argument @ 'getPlayerName' [Expected element at argu

ment 1, got nil]

[18:21:39] WARNING: customjq\script.lua:15: Bad argument @ 'getElementData' [Expected element at arg

ument 1, got nil]

[18:21:39] WARNING: customjq\script.lua:17: Bad argument @ 'getElementData' [Expected element at arg

ument 1, got nil]

[18:21:39] WARNING: customjq\script.lua:24: Bad argument @ 'setElementData' [Expected element at arg

ument 1, got nil]

function setPlayerState(thePlayer) 
     local elementdata = getElementData(thePlayer,"data.joindataold") 
     if (elementdata) then 
          return 
     else 
          setElementData(thePlayer, "data.joindatanew", "player.newplayer") 
     end 
end 
addEventHandler("onPlayerJoin",root,setPlayerState) 
  
  
function checkPlayerState(thePlayer) 
     local check = getPlayerSerial(player) 
     local playername = getPlayerName(thePlayer) 
     if getElementData(thePlayer, "data.joindataold") then 
          outputChatBox(playername.. "has joined the game [Regular Player]",0,190,90) 
     elseif getElementData(thePlayer, "data.joindatanew") then 
          outputChatBox(playername.. "has joined the game [New Player]",0,190,90) 
     end 
end 
addEventHandler("onPlayerJoin",root,checkPlayerState) 
  
function savePlayerData(thePlayer) 
     setElementData(thePlayer,"player.serial",check) 
end 
addEventHandler("onPlayerJoin",root,savePlayerData) 

Link to comment

okay so,now my code is like this.

function setPlayerState() 
     local elementdata = getElementData(source,"data.joindataold") 
     if (elementdata) then 
          return 
     else 
          setElementData(source, "data.joindatanew", "player.newplayer") 
     end 
end 
addEventHandler("onPlayerJoin",root,setPlayerState) 
  
  
function checkPlayerState() 
     local check = getPlayerSerial(source) 
     local playername = getPlayerName(source) 
     if getElementData(source, "data.joindataold") then 
          outputChatBox(playername.. "has joined the game [Regular Player]",0,190,90) 
     elseif getElementData(source, "data.joindatanew") then 
          outputChatBox(playername.. "has joined the game [New Player]",source,0,190,90) 
     end 
end 
addEventHandler("onPlayerJoin",root,checkPlayerState) 
  
function savePlayerData() 
     setElementData(source,"player.serial",check) 
end 
addEventHandler("onPlayerJoin",root,savePlayerData) 

it always considers me as new player,and the funny thing is that 1st outputChatBox is same as 2nd one,only text is different but it gave an error that it got '0' as 2nd argument. i just dont know what the fuck is this

i had to put 'source' in that 2nd outputChatBox and now nobody else can see when new player joins the game as it outputs the message only to the source(new joined player) please help me,this is confusing me too much

edit: in order to fix this new player and regular player thing i've added few lines of code

function makeRegularPlayer() 
if getElementData(source,"data.joindatanew") then 
setTimer(function() 
setElementData(source,"data.joindataold",check) -- line 31 
end,5000,1) 
end 
end 
addEventHandler("onPlayerJoin",root,makeRegularPlayer) 

[19:26:53] WARNING: customjq\script.lua:31: Bad argument @ 'setElementData' [Expected element at arg

ument 1, got nil]

so now i got a problem with this error,and outputchatbox(2nd one)

Link to comment

Alright, first things first. When using outputChatBox, the second argument tells you who the chat is visible to. If you want the chat to be visible to everyone, you need to use getRootElement() as argument. Look at serverside example #2 here:

https://wiki.multitheftauto.com/wiki/OutputChatBox

You've also got three functions that are all triggered when a player joins. If you are not using these functions for anything else you can merge them into one function. I think you will achieve the same thing when you use just one key.

    function setPlayerState() 
        local playername = getPlayerName(source) 
        if (getElementData(source, "regularPlayer")) then -- if this is not defined it will return false 
            outputChatBox(playername.. "has joined the game [Regular Player]",getRootElement(),0,190,90) 
        else 
            setElementData(source,"regularPlayer",true) -- now the data corresponding to key 'regularPlayer' is defined and will return true the next time this player logs in 
            outputChatBox(playername.. "has joined the game [New Player]",getRootElement(),0,190,90) 
        end 
    end 
    addEventHandler("onPlayerJoin",root,setPlayerState) 

Didn't test this, but this should achieve a similar goal

Link to comment
Because data stored using Element Data is deleted when the element is destroyed. A player is an element, when the player leaves - the element is destroyed. And the data gets deleted.

For permanent data storage, use setAccountData and getAccountData. The way they work are pretty much the same, but you use account element instead of player element.

What about guest users?it will only say new player for newly registered players,and not for new guests

Link to comment

Well if you want it to be a able to recognize whether a guest is new or not - I guess you could store their Serial, and then have it checked everytime someone decides to play as a guest. If the serial exists - regular player. If it does not exist - new player.

isGuestAccount 
getPlayerSerial 

Link to comment

tried this

function noNameYet(_,playerAccount) 
local playerAccount = getPlayerAccount(source) 
local playerSerial = getPlayerSerial(source) 
local playerNick = getPlayerName(source) 
   if (playerAccount) and isGuestAccount(source) then 
      if (serial) then 
         setAccountData(source,playerAccount,"player.guest",playerSerial) 
         else 
         outputChatBox("could not load" ..playerNick.. "'s serial",getRootElement(),255,0,0) 
      end 
   end 
end 
addEventHandler("onPlayerJoin",root,noNameYet) 
  
function otherFunction(_,playerAccount) 
local getGuestAccount = getAccountData(source) 
   if (getGuestAccount [source,playerAccount,"player.guest",playerSerial] ) then 
      outputChatBox(playerNick.. "has joined the game [New Player]",getRootElement(),0,190,0) 
      setAccountData(source,playerAccount,"player.regular",playerSerial) 
   end 
end 
addEventHandler("onPlayerJoin",root,otherFunction) 
  
function otherState(_,playerAccount) 
   if (getGuestAccount [source,playerAccount,"player.regular",playerSerial]) then 
      outputChatBox(playerNick.. "has joined the game [Regular Player]",getRootElement(),0,190,0) 
   end 
end 
addEventHandler("onPlayerJoin",root,otherState) 

[14:35:11] SCRIPT ERROR: customjq\script.lua:17: ']' expected near ','

[14:35:11] ERROR: Loading script failed: customjq\script.lua:17: ']' expected near ','

Link to comment

Also take note that Guest Accounts are also destroyed upon the player leaving the server. You'd be better off to check if the player is a guest, if true, save their Serial to either a XML or text file. You can also use SQL, which might be the preferred way by most.

Read through said file every time a guest joins. There are of course other ways of doing this.

Link to comment

i dont know which functions i had to use so i had to guess,

i didnt use sql because i dont know a single thing about it,and with fileWrite ofc i failed.

local playerAccount = getPlayerAccount(source) 
local playerSerial = getPlayerSerial(source) 
local playerNick = getPlayerName(source) 
local fileSerial = fileCreate("test.txt") 
  
function savePlayerSerial(_,playerAccount) 
   if (playerAccount) and isGuestAccount(source) then 
      if fileSerial then 
         fileWrite(fileSerial, playerNick, " - ", playerSerial ) 
         fileClose(fileSerial) 
      end 
   end 
end 
addEventHandler("onPlayerJoin",root,savePlayerSerial) 

[18:45:52] WARNING: customjq\script.lua:1: Bad argument @ 'getPlayerAccount' [Expected element at argument

1,got nil]

[18:45:52] WARNING: customjq\script.lua:2: Bad argument @ 'getPlayerSerial' [Expected player at argument 1, got nil]

[18:45:52] WARNING: customjq\script.lua:3: Bad argument @ 'getPlayerName' [Expected element at argument 1, got nil]

i just wanted to do a test to see will this even work before writing other part of code,but ofc i made probably an epic fail.

please help me i dont understand this

Link to comment

This will save all player serials in a table. Every time someone joins it will check their serials against the ones in the table. Every time you restart your server the table will be cleared, and everyone will be new again. If you want something more permanent you'll need to the things mentioned above.

playerSerials = {} -- create a table that will contain all the player serials 
  
function setPlayerState() 
    local playerSerial = getPlayerSerial(source) 
    local playerName = getPlayerName(source) 
     
    for index,theSerial in ipairs(playerSerials) do -- go through the table 
        if (playerSerial == theSerial) then -- if one of the serials matches the current player 
            outputChatBox(playerName.. " has joined the game [Regular Player]",getRootElement(),0,190,90) -- the player is regular 
            return --and we can stop 
        end 
    end 
     
    outputChatBox(playerName.. "has joined the game [New Player]",getRootElement(),0,190,90) -- if no serial matches, the player is new 
    table.insert(playerSerials,playerSerial) --add them to the table so they won't be new next time 
end 
addEventHandler("onPlayerJoin",root,setPlayerState) 

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