Jump to content

Aviso SetElementData


Recommended Posts

function onPlayerQuit ( )
      local playeraccount = getPlayerAccount ( source )
      if ( playeraccount ) and not isGuestAccount ( playeraccount ) then
            local playerfood = getElementData(source, "food")
            setAccountData ( playeraccount, "hud.food", playerfood )
			outputDebugString (  getPlayerName(source).. " - [SALVO FOOD: "..playerfood.."]" )
      end
end
 
function onPlayerLogin (_, playeraccount )
      if ( playeraccount ) then
            local playerfood = getAccountData ( playeraccount, "hud.food" )
            if ( playerfood ) then
				  setTimer(function()
				  setElementData(source,"food", playerfood) 
				  outputDebugString (  playerfood )
				end,500,1)
            end
      end
end
 
addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit )
addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin )

NÃO ESTOU CONSEGUINDO ENTENDER ESSE WARNING MDS!

QUEM PODER ME AJUDAR...

WARNING que aparece:

xCHzQgD.png

Edited by ~#Pedro
Link to comment

[RESOLVIDO]

Aqui esta o código para quem tiver a mesma duvida de save em SetElementData

Créditos (Meus) Pedro861

 

function onPlayerQuit ( )
      local playeraccount = getPlayerAccount ( source )
      if ( playeraccount ) and not isGuestAccount ( playeraccount ) then
            local playerfood = getElementData(source, "food")
            setAccountData ( playeraccount, "hud.food", playerfood )
			outputDebugString (  getPlayerName(source).. " - [SALVO FOOD: "..playerfood.."]" )
      end
end
 
function onPlayerLogin (_, playeraccount,thePlayer )
 local thePlayer = getAccountPlayer ( playeraccount )
    if ( getElementType ( thePlayer ) == "player" ) then 
		  if ( playeraccount ) then
				local playerfood = getAccountData ( playeraccount, "hud.food" )
				if ( playerfood ) then
					  setTimer(function()
					  setAccountData( playeraccount,"hud.food",tonumber( getAccountData(playeraccount,"hud.food") or 0 ) )  -- Não ha necessidade de ter essa linha, coloquei só por prevenção.
					  setElementData( thePlayer, "food",tonumber( getAccountData(playeraccount,"hud.food") or 0 ) ) 
					  outputDebugString (  playerfood )
					end,500,1)
				end
            end
      end
end
 
addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit )
addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin )

 

OBS: É um sistema save não há porque escode-lo de quem precisa!

Edited by ~#Pedro
Link to comment
  • Moderators

Esse erro era porque você criou outra função, lá no setTimer, aí source retornava nulo. Nesse caso, você precisa passar o jogador no parâmetro, desta forma:

function onPlayerLogin (_, playeraccount )
	if ( playeraccount ) then
		local playerfood = getAccountData ( playeraccount, "hud.food" )
		if ( playerfood ) then
			setTimer( function( p )
				setElementData(p,"food", playerfood) 
				outputDebugString (  playerfood )
			end, 500, 1, source )
		end
	end
end

 

Edited by DNL291
Link to comment
19 hours ago, DNL291 said:

Esse erro era porque você criou outra função, lá no setTimer, aí source retornava nulo. Nesse caso, você precisa passar o jogador no parâmetro, desta forma:


function onPlayerLogin (_, playeraccount )
	if ( playeraccount ) then
		local playerfood = getAccountData ( playeraccount, "hud.food" )
		if ( playerfood ) then
			setTimer( function( p )
				setElementData(p,"food", playerfood) 
				outputDebugString (  playerfood )
			end, 500, 1, source )
		end
	end
end

 

Muito obrigado por tentar ajudar, mas ja resolvi, até postei ai nos comentários ja que não consegui editar o topic

Link to comment
  • Moderators
7 hours ago, ~#Pedro said:

Muito obrigado por tentar ajudar, mas ja resolvi, até postei ai nos comentários ja que não consegui editar o topic

Acho que me entendeu mal. Eu vi que já resolveu usando: local playeraccount = getPlayerAccount ( source ). E nada contra sobre ter funcionado, se tá tudo OK, parabéns por conseguir arrumar o código, dessa forma que aprende Lua na verdade.

Eu disse sobre aquele erro "Expected element at argument 1, got nil", era por causa do: setTimer(function()... Isso cria outra função, e source passa a ser inválido. Mas passando o elemento para a função criada em setTimer vai resolver, como eu disse. Evita definir uma variável que ocupa mais memória, a na verdade é a solução mais lógica.

  • Like 1
Link to comment
16 hours ago, DNL291 said:

Acho que me entendeu mal. Eu vi que já resolveu usando: local playeraccount = getPlayerAccount ( source ). E nada contra sobre ter funcionado, se tá tudo OK, parabéns por conseguir arrumar o código, dessa forma que aprende Lua na verdade.

Eu disse sobre aquele erro "Expected element at argument 1, got nil", era por causa do: setTimer(function()... Isso cria outra função, e source passa a ser inválido. Mas passando o elemento para a função criada em setTimer vai resolver, como eu disse. Evita definir uma variável que ocupa mais memória, a na verdade é a solução mais lógica.

Exatamente, acho que muitos aprendem errado dessa forma pensando "se está funcionando está certo.".

É bom tentar sempre otimizar o código.

  • Like 2
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...