Jump to content

Erro no elemento source


Recommended Posts

Opa, estou tentando solucionar o erro de todos os mods do meu servidor, porém apareceu um aqui que eu não consigo solucionar, aparentemente não vi nada de errado, alguém poderia me ajudar a solucionar isso? pfv

WARNING: Loja1\server.lua:25: Bad argument @ 'givePlayerMoney' [Expected element at argument 1]
WARNING: Loja1\server.lua:26: Bad argument @ 'outputChatBox' [Expected element at argument 2]

 

function comandoloja (source)
if isElementWithinMarker (source, roubarloja ) then
for _,players in pairs(getElementsByType("player")) do
                for i, k in pairs(corps) do 
                local ContaACL = getAccountName ( getPlayerAccount ( players ) )                
                if isObjectInACLGroup ("user."..ContaACL, aclGetGroup (k)) then
    outputChatBox("#ffffff*#FF0000Atenção - O Jogador:#ffffff " .. getPlayerName(source) .. " #FF0000Está Roubando a Loja1.", players,255,255,255,true)
end
end
end
setPlayerWantedLevel ( source, 6 )
setElementPosition ( lojasair1, 834.66760, 7.45359, 1050)
setElementPosition ( roubarloja, 822.74316, -0.47490, 1050)
outputChatBox ('#ffffffAguarde Enquanto Você Pega O Dinheiro!',source,255,255,255,true)
setPedAnimation(source, "SHOP", "ROB_Loop_Threat", 40000, true, false, false, false)
setPedRotation(source,90 )
setPedAnimation( ped, "SHOP", "SHP_Rob_HandsUp")
triggerClientEvent ( root, "roubar", root)
setElementVisibleTo ( blipalarme, root, true )
setTimer ( function()
setElementPosition ( lojasair1, 834.664418,7.3,1005.18701)
setElementInterior ( roubarloja, 3)
setPedAnimation(ped,false)
setElementPosition ( ped, 819.40869, -0.45965, 1004.17969)
givePlayerMoney ( source, 1300 )
outputChatBox ('#ffffffVocê Roubou a Loja 1 e Pegou #00ff00R$1.000',source,255,255,255,true)
    end, 40000, 1 )
setTimer ( function()
   setElementVisibleTo ( blipalarme, root, false )
   setElementPosition( roubarloja, 822.74316, -0.47490, 1003.2)
    end, 80000, 1 )
end
end    
addCommandHandler ( "assaltar", comandoloja )

 

Link to comment
  • Moderators

Tente:

function comandoloja (source)
	if isElementWithinMarker (source, roubarloja ) then
		for _,players in pairs(getElementsByType("player")) do
			for i, k in pairs(corps) do 
				local ContaACL = getAccountName ( getPlayerAccount ( players ) )                
				if isObjectInACLGroup ("user."..ContaACL, aclGetGroup (k)) then
					outputChatBox("#ffffff*#FF0000Atenção - O Jogador:#ffffff " .. getPlayerName(source) .. " #FF0000Está Roubando a Loja1.", players,255,255,255,true)
				end
			end
		end
		setPlayerWantedLevel ( source, 6 )
		setElementPosition ( lojasair1, 834.66760, 7.45359, 1050)
		setElementPosition ( roubarloja, 822.74316, -0.47490, 1050)
		outputChatBox ('#ffffffAguarde Enquanto Você Pega O Dinheiro!',source,255,255,255,true)
		setPedAnimation(source, "SHOP", "ROB_Loop_Threat", 40000, true, false, false, false)
		setPedRotation(source,90 )
		setPedAnimation( ped, "SHOP", "SHP_Rob_HandsUp")
		triggerClientEvent ( root, "roubar", root)
		setElementVisibleTo ( blipalarme, root, true )
		
		setTimer ( function(player)
			setElementPosition ( lojasair1, 834.664418,7.3,1005.18701)
			setElementInterior ( roubarloja, 3)
			setPedAnimation(ped,false)
			setElementPosition ( ped, 819.40869, -0.45965, 1004.17969)
			givePlayerMoney ( player, 1300 )
			outputChatBox ('#ffffffVocê Roubou a Loja 1 e Pegou #00ff00R$1.000',player,255,255,255,true)
		end, 40000, 1, source )
		
		setTimer ( function()
			setElementVisibleTo ( blipalarme, root, false )
			setElementPosition( roubarloja, 822.74316, -0.47490, 1003.2)
		end, 80000, 1 )
	end
end    
addCommandHandler ( "assaltar", comandoloja )

 

  • Thanks 1
Link to comment

Ele está dando isso, tentei também trocar para thePlayer e mesmo assim continuou

WARNING: Loja1\server.lua:26: Bad argument @ 'givePlayerMoney' [Expected element at argument 1, got nil]

Edited by WeeD1
Link to comment
1 hour ago, WeeD1 said:

Ele está dando isso, tentei também trocar para thePlayer e mesmo assim continuou

WARNING: Loja1\server.lua:26: Bad argument @ 'givePlayerMoney' [Expected element at argument 1, got nil]

Tente isso:
 

function comandoloja (source)
	if isElementWithinMarker (source, roubarloja ) then
		for _,players in pairs(getElementsByType("player")) do
			for i, k in pairs(corps) do 
				local ContaACL = getAccountName ( getPlayerAccount ( players ) )                
				if isObjectInACLGroup ("user."..ContaACL, aclGetGroup (k)) then
					outputChatBox("#ffffff*#FF0000Atenção - O Jogador:#ffffff " .. getPlayerName(source) .. " #FF0000Está Roubando a Loja1.", players,255,255,255,true)
				end
			end
		end
		setPlayerWantedLevel ( source, 6 )
		setElementPosition ( lojasair1, 834.66760, 7.45359, 1050)
		setElementPosition ( roubarloja, 822.74316, -0.47490, 1050)
		outputChatBox ('#ffffffAguarde Enquanto Você Pega O Dinheiro!',source,255,255,255,true)
		setPedAnimation(source, "SHOP", "ROB_Loop_Threat", 40000, true, false, false, false)
		setPedRotation(source,90 )
		setPedAnimation( ped, "SHOP", "SHP_Rob_HandsUp")
		triggerClientEvent ( root, "roubar", root)
		setElementVisibleTo ( blipalarme, root, true )
		
		setTimer ( function(source)
			setElementPosition ( lojasair1, 834.664418,7.3,1005.18701)
			setElementInterior ( roubarloja, 3)
			setPedAnimation(ped,false)
			setElementPosition ( ped, 819.40869, -0.45965, 1004.17969)
			givePlayerMoney ( source, 1300 )
			outputChatBox ('#ffffffVocê Roubou a Loja 1 e Pegou #00ff00R$1.000',source,255,255,255,true)
		end, 40000, 1, source )
		
		setTimer ( function()
			setElementVisibleTo ( blipalarme, root, false )
			setElementPosition( roubarloja, 822.74316, -0.47490, 1003.2)
		end, 80000, 1 )
	end
end    
addCommandHandler ( "assaltar", comandoloja )

EDIT 1: Não testei!

Edited by Tommy.
  • Thanks 1
Link to comment
7 hours ago, Lord Henry said:

O erro acontece pois dentro do setTimer, o source deixa de existir. Pois ele pertence somente à função principal e no setTimer é outra função.

Como a função do timer está dentro de uma função principal as variáveis da principal são acessíveis também nela.

Link to comment
  • Other Languages Moderators
3 hours ago, MaligNos said:

Como a função do timer está dentro de uma função principal as variáveis da principal são acessíveis também nela.

Não quando a variável é parâmetro de função, ela só funciona dentro dela mesma. Por isso é necessário passar o source para a função do setTimer, pois naturalmente ela não vai funcionar. Ou então vc coloca o source em uma variável local e dai usa essa variável local dentro da função do setTimer, que por estar dentro do escopo da função principal, dai funciona.

Link to comment
27 minutes ago, Lord Henry said:

Não quando a variável é parâmetro de função, ela só funciona dentro dela mesma. Por isso é necessário passar o source para a função do setTimer, pois naturalmente ela não vai funcionar. Ou então vc coloca o source em uma variável local e dai usa essa variável local dentro da função do setTimer, que por estar dentro do escopo da função principal, dai funciona.

Acabei de testar:

-- https://www.lua.org/cgi-bin/demo

function teste(var)
  function teste2()
    print(var)
  end
  teste2()
end

teste("oie")
-- output: oie

 

Link to comment
  • Other Languages Moderators

Teste isso então:

function teste (enteringPlayer, seat, jacked, door) -- source = veículo
	setTimer (function ()
		print (getElementModel(source)) -- ERRO: source = nil
	end, 100, 1)
end
addEventHandler ("onVehicleStartEnter", getRootElement(), teste)
function teste (enteringPlayer, seat, jacked, door) -- source = veículo
	-- setTimer (function ()
		print (getElementModel(source)) -- source = veículo
	-- end, 100, 1)
end
addEventHandler ("onVehicleStartEnter", getRootElement(), teste)

 

1 hour ago, Lord Henry said:

Não quando a variável é parâmetro de função, ela só funciona dentro dela mesma. Por isso é necessário passar o source para a função do setTimer, pois naturalmente ela não vai funcionar. Ou então vc coloca o source em uma variável local e dai usa essa variável local dentro da função do setTimer, que por estar dentro do escopo da função principal, dai funciona.

No caso me equivoquei quando eu disse parâmetro de função. Na verdade só perde se for source de evento.

Edited by Lord Henry
Link to comment
  • Other Languages Moderators
function teste (enteringPlayer, seat, jacked, door) -- source = veículo
	setTimer (function ()
		print (getElementModel(enteringPlayer)) -- enteringPlayer ainda existe. Mas o source do evento não.
		print (getElementModel(source)) -- ERRO: source = nil
	end, 100, 1)
end
addEventHandler ("onVehicleStartEnter", getRootElement(), teste)

 

Edited by Lord Henry
Link to comment

Não é muito lógico ele deixar de existir, dei uma olhada aqui e notei que e o source só se “perde” na função do setTimer:

function teste (enteringPlayer, seat, jacked, door) -- source = veículo
	(function()
		print (getElementModel(source)) -- ok
	end)()

	setTimer (function ()
		print (getElementModel(source)) -- fail
	end, 100, 1)
end
addEventHandler ("onVehicleStartEnter", getRootElement(), teste)

As variáveis globais do addEventHandler: this, client, eventName, etc, também deixam de existir. Meu palpite é que no código fonte do setTimer existe algum método resetando essas variáveis (talvez aqui).

No caso do tópico em que não existe a variável predefinida source de um evento, mas sim uma variável que foi declarada com o mesmo nome no parâmetro, acaba por não influenciar:

addCommandHandler("oie",
	function(source)
		setTimer (function ()
			print(source) -- ok
		end, 100, 1)
	end
)

 

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