Jump to content

[AJUDA] Salvar Na Conta


Recommended Posts

Olá galera, tenho um script que calcula a distancia percorrida pelo jogador no veículo e exibe na tela, como um odômetro, porém eu queria que esses dados da distancia ficassem salvos na conta do jogador, pra caso ele saia do server,  quando retornasse estivesse lá

tentei isso, mas não funcionou:

-------------------------------------------------- CLIENT

local font1 = dxCreateFont("files/RobotoCondensed.ttf", 35)

local SizeX, SizeY = guiGetScreenSize()
local px,py = 1920,1080
local x,y = (SizeX/px), (SizeY/py)

function speedometrs ()
	if getElementData(getLocalPlayer(), "hud") == true then return end
    local veh = getPedOccupiedVehicle(getLocalPlayer()) 
    if not veh or getVehicleOccupant ( veh ) ~= localPlayer then return true end
    if not driveDistance then lastTick = getTickCount() driveDistance = getElementData(localPlayer, "driveDistance") or 0 end
	local neux, neuy, neuz = getElementPosition(veh)

    dxDrawText(""..tostring(math.round(driveDistance),1).." км",SizeX,SizeY - 40, SizeX - 360,SizeY, tocolor(255,255,255,180), 0.3,0.3, font1,"center","center")

	if not altx or not alty or not altz then
		altx, alty, altz=getElementPosition(veh)
	end
	local driveTotal = getDistanceBetweenPoints3D(neux,neuy,neuz,altx,alty,altz)
	driveTotal = driveTotal/400
	altx,alty,altz=neux,neuy,neuz
	driveDistance = math.round(driveDistance+driveTotal,3)
	if lastTick+5000 < getTickCount() then
		lastTick = getTickCount()
		setElementData(localPlayer, "driveDistance", driveDistance)
	end
	
end
addEventHandler("onClientRender", root, speedometrs)

function math.round(number, decimals, method)
    decimals = decimals or 0
    local factor = 10 ^ decimals
    if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor
    else return tonumber(("%."..decimals.."f"):format(number)) end
end

addEventHandler("onClientPlayerVehicleEnter",getRootElement(),function(veh,seat)
	if source == localPlayer then
		altx, alty, altz=getElementPosition(veh)
		lastTick = getTickCount()
		driveDistance = getElementData(localPlayer, "driveDistance")
	end
end)

 

--SERVER
function CarregarLoginPlay ( conta )
	if not isGuestAccount ( conta ) then
		if conta then			
			local source = getAccountPlayer ( conta )
			local emp = getAccountData ( conta, "driveDistance" ) or "N/C"
			setElementData ( source, "driveDistance", emp )
		end
	end	
end

function SalvarLoginPlay ( conta )
	if conta then
	local Nome = getElementData ( source, "driveDistance" ) or "N/C"
	setAccountData ( conta, "driveDistance", Nome )
end

 

Link to comment

Tente usar isto

 

addEventHandler ( "onPlayerLogin", root,
  function ( _, acc )
	setTimer ( Carregar_Dis, 50, 1, acc )
  end
)

function Carregar_Dis ( conta )
	if not isGuestAccount ( conta ) then
		if conta then	
			local source = getAccountPlayer ( conta )
			local driveDistance = getAccountData ( conta, "driveDistance" ) or 0
			setElementData ( source, "driveDistance", driveDistance )
		end
	end	
end

function Iniciar_Resource ( res )
	if res == getThisResource ( ) then
		for i, player in ipairs(getElementsByType("player")) do
			local acc = getPlayerAccount ( player )
			if not isGuestAccount ( acc ) then
				Carregar_Dis ( acc )
			end
		end
	end
end
addEventHandler ( "onResourceStart", getRootElement ( ), Iniciar_Resource )

function Salvar_Dis ( conta )
	if conta then
		local source = getAccountPlayer ( conta )
		local driveDistance = getElementData ( source, "driveDistance" ) or 0
		setAccountData ( conta, "driveDistance", driveDistance )
	end
end

function Desligar_Dis ( res )
    if res == getThisResource ( ) then
		for i, player in ipairs(getElementsByType("player")) do
			local acc = getPlayerAccount ( player )
			if not isGuestAccount ( acc ) then
				Salvar_Dis ( acc )
			end
		end
	end
end 
addEventHandler ( "onResourceStop", getRootElement(), Desligar_Dis )

function Sair_Dis_Servidor ( quitType )
	local acc = getPlayerAccount(source)
	if not isGuestAccount ( acc ) then
		if acc then
			Salvar_Dis ( acc )
		end
	end
end
addEventHandler ( "onPlayerQuit", getRootElement(), Sair_Dis_Servidor )

Não testei

  • Thanks 1
Link to comment

Você vai precisar obter os dados da sessão atual e somar com o atual na conta, senão ficará sempre o valor de uma sessão.
Na sua tentativa existem vários erros:

  • Falta um end na função SalvarLoginPlay
  • Não adicionou nenhum evento às funções
  • source que precisa ser um elemento-player

Segue o código:
 


-- carregamento
local function CarregarLoginPlay ( thePlayer )
    local acc = getPlayerAccount(thePlayer)
    if acc and not isGuestAccount(acc) then		
		local driveDisdata = getAccountData( acc, "driveDistance" ) or 0
		setElementData( thePlayer, "driveDistance", driveDisdata )
	end	
end
addEventHandler( "onPlayerLogin", root, function() setTimer(CarregarLoginPlay, 200, 1, source) end )

addEventHandler( "onResourceStart", resourceRoot, function()
    setTimer( function()
        for _,p in pairs(getElementsByType"player")
            CarregarLoginPlay(p)
        end
    end, 250, 1 )
end )

-- salvamento
local function SalvarLoginPlay ( thePlayer, acc )
    local acc = (acc and acc or getPlayerAccount(thePlayer))
    if acc and not isGuestAccount(acc) then
        local acc_data = tonumber(getAccountData( acc, "driveDistance" )) or 0
        local element_data = tonumber(getElementData ( thePlayer, "driveDistance" )) or "N/C"
        if element_data ~= "N/C" then
            setAccountData( acc, "driveDistance", acc_data + element_data )
        else
            outputDebugString("@SalvarLoginPlay: 'element_data' retornou um valor inválido (jogador: "..getPlayerName(thePlayer)..")")
        end
    end
end
addEventHandler( "onPlayerQuit", root, function() SalvarLoginPlay(source) end )
addEventHandler( "onPlayerLogout", root, function(acc) SalvarLoginPlay(source, acc) end )

addEventHandler( "onResourceStop", resourceRoot, function()
    setTimer( function()
        for _,p in pairs(getElementsByType"player")
            SalvarLoginPlay(p)
        end
    end, 250, 1 )
end )

Não testei; recomendo que ative o debugscript 3 ao testar

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