Jump to content

[SPA]Recuperar datos onResourcestop[ENG]Retrieve accountdata on resource restart


Recommended Posts

spanish:

Hola, puedes hacerme el favor de ayudarme con este script, ya logré recuperar el equipo anterior cuando salí del servidor pero ahora no consigo que el equipo me salve al reiniciar el recurso adjunto al código del script

 

english:

Hello, can you do me the favor of helping me with this script, I already managed to recover the previous team when I left the server but now I cannot get the team to save me when restarting the resource attached to the script code

 

server

empresa = createTeam("EMPRESA", 0, 255, 0) --team
  

oldTeam = {}        -- Tabela para salvar o Team antigo.


addEvent('setSERV',true)  --contratar,
addEventHandler('setSERV',root,
function()
	local playerTeam = getPlayerTeam(source)
	if playerTeam then
		local serial = getPlayerSerial(source)
		oldTeam[serial] = getTeamName(playerTeam)
		setPlayerTeam ( source, empresa )
	else
		setPlayerTeam ( source, empresa )	
	end
end)

addEvent('setSERVNo',true) --despedir
addEventHandler('setSERVNo',root, 
function(_)
	local playeraccount = getPlayerAccount ( source )
	local serial = getPlayerSerial(source)
	LoadTeam = getAccountData ( playeraccount, "oldTeam" )
	if oldTeam[serial] and getTeamFromName(oldTeam[serial]) then
		setPlayerTeam(source, getTeamFromName(oldTeam[serial])) 
	elseif ( LoadTeam ) then
		teamLoad = getTeamFromName ( LoadTeam ) 
		setPlayerTeam ( source, teamLoad )
	else
		setPlayerTeam ( source, nil )		
	end
end)

function onPlayQuit ( )
	local playeraccount = getPlayerAccount ( source )
	if ( playeraccount ) and not isGuestAccount ( playeraccount ) then -- if the player is logged in
		local serial = getPlayerSerial(source)
		if oldTeam[serial] and getTeamFromName(oldTeam[serial]) then	
		  setAccountData ( playeraccount, "oldTeam", getTeamFromName(oldTeam[serial]) ) -- save it in his account
	end
end
end
addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayQuit )

function onResstop ( )
	local playeraccount = getPlayerAccount ( source )
	if ( playeraccount ) and not isGuestAccount ( playeraccount ) then -- if the player is logged in
		local serial = getPlayerSerial(source)
		if oldTeam[serial] and getTeamFromName(oldTeam[serial]) then	
		  setAccountData ( playeraccount, "oldTeam", getTeamFromName(oldTeam[serial]) ) -- save it in his account
	end
end
end
addEventHandler ( "onResourceStop", getRootElement ( ), onResstop )

 

Link to comment
  • Discord Moderators

Hello

You need to go through all players, with ipairs loop and getElementsByType, when the resource stops

Then you can check for account and save oldTeam

function onResstop()
	-- Loop all players
	for k,player in ipairs(getElementsByType("player")) do
		local playeraccount = getPlayerAccount(player)
		if ( playeraccount ) and not isGuestAccount ( playeraccount ) then -- if the player is logged in
			local serial = getPlayerSerial(player)
			if oldTeam[serial] and getTeamFromName(oldTeam[serial]) then	
			  setAccountData ( playeraccount, "oldTeam", getTeamFromName(oldTeam[serial]) ) -- save it in his account
			end
		end
	end
end
addEventHandler("onResourceStop", resourceRoot, onResstop)

And you should bind "onResourceStop" to the resourceRoot, if you bind it to the root element, it will trigger when any resource stops.

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