Jump to content

[HELP] How to save timer when a player quit from server?


Turbesz

Recommended Posts

i want create a jail system, but when a player reconnect to the server, the countdown stops at the last value...

for example: the player have 10 minutes left, then reconnects, the timer does not subtract from the number anymore.. how to fix this?

code:

rRoot = getResourceRootElement(getThisResource())

local idozito = {}

function JAIL(thePlayer,commandName,sendToName,ido,...)
	if ( sendToName ) then
		if (getPlayerFromParticalName (sendToName)) and ido then
			if ( not tonumber ( ido ) ) then 
				triggerClientEvent(thePlayer,"chat->NewMessage",thePlayer,"hiba","Adj meg egy számot 0-9999 között időnek. Ennyi percre kerül be a játékos.")

				return end
			if tonumber(ido) > 9999 then 
					triggerClientEvent(thePlayer,"chat->NewMessage",thePlayer,"hiba","Maximum 9999 percre teheted be börtönbe a játékost.")
	
			return end
			local toPlayer = getPlayerFromParticalName (sendToName)
			local pmMessage = table.concat( { ... }, " " )
				if not (pmMessage == "") then
				triggerClientEvent(thePlayer,"chat->NewMessage",thePlayer,false,"#FFb540[Börtön] "..getPlayerName(thePlayer).." #FfFfFfbebörtönözte #FFb540"..getPlayerName(toPlayer).." #FFffFFjátékost, #FFb540"..ido.." #FFffFFpercre.")
				triggerClientEvent(thePlayer,"chat->NewMessage",thePlayer,false,"#ffb540[Börtön] #FFffFFIndok: #FFb540"..pmMessage)
				setElementData(toPlayer,"bortonperc",ido)
				idozito[toPlayer] = setTimer(function()
				local percei = getElementData(toPlayer,"bortonperc") or 0
				if percei == 0 then 
					if isTimer(idozito[toPlayer]) then 
						killTimer(idozito[toPlayer]) 
					end
					triggerClientEvent(thePlayer,"chat->NewMessage",toPlayer,false,"#ffb540vége")
				end
				setElementData(toPlayer,"bortonperc",percei-1)
				end,5000,0)
				else

					return false
				end
		else
			triggerClientEvent(thePlayer,"chat->NewMessage",thePlayer,"hiba","A megadott játékos nem található.")

			return false
		end
	else
		return false
	end
end
addCommandHandler("jail", JAIL)

function teszt(thePlayer)
	local percei = getElementData(thePlayer,"bortonperc") or 0

	outputChatBox(percei)
end
addCommandHandler("teszt",teszt)


function getPlayerFromParticalName(thePlayerName)
	local thePlayer = getPlayerFromName(thePlayerName)
	if thePlayer then
		return thePlayer
	end
	for _,thePlayer in ipairs(getElementsByType("player")) do
		if string.find(string.gsub(getPlayerName(thePlayer):lower(),"#%x%x%x%x%x%x", ""), thePlayerName:lower(), 1, true) then
			return thePlayer
		end
	end
return false
end

function onPlayerQuit ( )
	local playeraccount = getPlayerAccount ( source )
	if ( playeraccount ) then
		  local ezezeze = getElementData(source,"bortonperc") or 0
		  setAccountData (playeraccount, "bortonperc", ezezeze)
	end
end
addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit )

local root = getRootElement()
addEventHandler("onPlayerLogin", root,
function()
local playeraccount = getPlayerAccount ( source )
	if ( playeraccount ) then
		  local azazaza = getAccountData (playeraccount, "bortonperc") or 0
		  setElementData(source,"bortonperc",azazaza)
		  setTimer(function()
		  if azazaza ~= 0 or azazaza ~= -1 then
			idozito[source] = setTimer(function()
			if azazaza == 0 then 
				if isTimer(idozito[source]) then 
						killTimer(idozito[source]) 
				end
					triggerClientEvent(source,"chat->NewMessage",source,false,"#ffb540vége")
			end
				setElementData(source,"bortonperc",azazaza-1)
				outputChatBox(azazaza)
			end,5000,0)
		end
	end,1000,1)
	end
end
)

 

Link to comment
  • Moderators
2 hours ago, Turbesz said:

for example: the player have 10 minutes left, then reconnects, the timer does not subtract from the number anymore.. how to fix this?

 

Save(and load) it based on serial.

idozito[toPlayer] = setTimer(function()

idozito[getPlayerSerial(toPlayer)] = setTimer(function()
				

 

Edited by IIYAMA
Link to comment
  • Moderators
8 minutes ago, Turbesz said:

i tried this, but same problem

You can also use the account (pointer) or accountName instead.

You may have tried it, but it seems like you didn't do it correctly.

 

Try to use the accountName (since it seems you are using that) and if it doesn't work, post the code again with the changes.

Link to comment
5 minutes ago, IIYAMA said:

You can also use the account (pointer) or accountName instead.

You may have tried it, but it seems like you didn't do it correctly.

 

Try to use the accountName (since it seems you are using that) and if it doesn't work, post the code again with the changes.

now does not work the save part of script

function onPlayerQuit ( )
	local playeraccount = getAccountName ( source )
	if ( playeraccount ) then
		  local ezezeze = getElementData(source,"bortonperc") or 0
		  setAccountData (playeraccount, "bortonperc", ezezeze)
	end
end
addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit )

local root = getRootElement()
addEventHandler("onPlayerLogin", root,
function()
local playeraccount = getAccountName ( source )
	if ( playeraccount ) then
		  local azazaza = getAccountData (playeraccount, "bortonperc") or 0
		  setElementData(source,"bortonperc",azazaza)
		  setTimer(function()
		  if azazaza ~= 0 or azazaza ~= -1 then
			idozito[getPlayerSerial(source)] = setTimer(function()
			if azazaza == 0 then 
				if isTimer(idozito[source]) then 
						killTimer(idozito[source]) 
				end
					triggerClientEvent(source,"chat->NewMessage",source,false,"#ffb540vége")
			end
				setElementData(source,"bortonperc",azazaza-1)
				outputChatBox(azazaza)
			end,5000,0)
		end
	end,1000,1)
	end
end
)

 

Link to comment
  • Moderators
Just now, Turbesz said:

if isTimer(idozito[source]) then killTimer(idozito[source]) end

This is still old.

 


 

Source is not available in async functions (created by setTimer). Therefore you need to redefine it before using the async functions.

local thePlayer = source

 


 

4 minutes ago, Turbesz said:

function onPlayerQuit ( ) local playeraccount = getAccountName ( source ) if ( playeraccount ) then local ezezeze = getElementData(source,"bortonperc") or 0 setAccountData (playeraccount, "bortonperc", ezezeze) end end addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit )

There is no timer saved here.

 

Link to comment
8 minutes ago, IIYAMA said:

This is still old.

 


 

Source is not available in async functions (created by setTimer). Therefore you need to redefine it before using the async functions.


local thePlayer = source

 


 

There is no timer saved here.

 

still does not work o.O 

local root = getRootElement()
addEventHandler("onPlayerLogin", root,
function()
local playeraccount = getAccountName ( source )
	if ( playeraccount ) then
		  local azazaza = getAccountData (playeraccount, "bortonperc") or 0
		  setElementData(source,"bortonperc",azazaza)
		  setTimer(function()
			local thePlayer = source
		  if azazaza ~= 0 or azazaza ~= -1 then
			idozito[getPlayerSerial(thePlayer)] = setTimer(function()
			if azazaza == 0 then 
				if isTimer(getPlayerSerial(thePlayer)) then 
						killTimer(getPlayerSerial(thePlayer)) 
				end
					triggerClientEvent(thePlayer,"chat->NewMessage",thePlayer,false,"#ffb540vége")
			end
				setElementData(thePlayer,"bortonperc",azazaza-1)
				outputChatBox(azazaza)
			end,5000,0)
		end
	end,1000,1)
	end
end
)

 

Link to comment
  • Moderators
1 minute ago, Turbesz said:

i didn't removed anything, just this part does not work now

In that case, swap these 2 lines:

setTimer(function()

local thePlayer = source

local thePlayer = source
setTimer(function()

 

 

And then start working on the other functions. Like this one:

function JAIL(thePlayer,commandName,sendToName,ido,...)

 

 

And add a debug command:

addCommandHandler("idozito", 
function () 
	iprint("idozito:",idozito)
end)

 

So that you can monitor your table: idozito

 

Link to comment
  • Moderators
5 hours ago, Turbesz said:

 

I've tried save the data many ways, but the timer after login it doesn't work at all, everything else works :/ 

Every line of code can be validated, if it does what it is suppose to do. By only looking at the end result will only give you one type of answer, which is: success or failure.

Note, this ain't a lecture. But telling me that something isn't working, without actually trying to figure out what went wrong through various debug techniques doesn't sit right with me. I know that it can be a pain to debug everything (that ain't death staring at a debug console), but now it feels more like unwillingness.

Please learn it, for your own sake.

Edited by IIYAMA
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...