Jump to content

SOLVED


Bean666

Recommended Posts

how could you save a timer onplayerquit and set it back right onplayerlogin? im using a timer like this, this is my first time trying to save a timer. so i'm kinda curious how?

any tips/help, appreciated.

local timer = {} 
  
function ex(source) 
if timer[source] and isTimer(timer[source]) then 
return end 
     timer[source] = setTimer(function() 
     timer[source] = nil 
     outputChatBox("blablablabla", source) 
     end, 3600*500, 1) 
     end 
addCommandHandler ( "ex", ex ) 

Edited by Guest
Link to comment

'source' is a userdata which is unique for every each element. so every time a player quits/joins they'll have a different userdata. you can save the timer's details with getTimerDetails function on player's account data when they quit and then work it after they join

Link to comment

EDIT: ah , doesn't work, any tips/help?

function onQuit ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
    local timerget = getTimerDetails( timer[source] ) 
     setAccountData ( playeraccount, "timercd", timerget ) 
     end 
        end 
    addEventHandler ( "onPlayerQuit", root, onQuit ) 

Link to comment

i did smth like this.

  
function onQuit ( ) 
    local playeraccount = getPlayerAccount ( source ) 
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then 
    local timerget = getTimerDetails( timer[source] ) 
     setAccountData ( playeraccount, "timercd", timerget ) 
     end 
        end 
    addEventHandler ( "onPlayerQuit", root, onQuit ) 
  
function onLogin () 
    if ( playeraccount ) then 
     local Timer = getAccountData ( playeraccount, "timercd" ) 
             if ( Timer ) then 
             timer[source] = setTimer(function() 
             timer[source] = nil 
             outputChatBox("You can now use the advert again", source) 
              end, Timer, 1) 
                end 
                   end 
                      end 
    addEventHandler ( "onPlayerLogin", getRootElement ( ), onLogin) 

Link to comment
  
function onLogin (_, playeraccount) 
    if ( playeraccount ) then 
     local Timer = getAccountData ( playeraccount, "timercd" ) 
             if ( Timer ) then 
             timer[source] = setTimer(function() 
             timer[source] = nil 
             outputChatBox("You can now use the advert again", source) 
              end, Timer, 1) 
                end 
                   end 
                      end 
    addEventHandler ( "onPlayerLogin", getRootElement ( ), onLogin) 

Link to comment

Here is all what you need.

local timerTable = {} -- timer table 
  
-- save the time on player quit 
function save () 
        local playerAccount = getPlayerAccount ( source ) -- get the player account 
        if (isTimer(timerTable[source])) then -- if the timer exist then  
        local remaining = getTimerDetails(timerTable[source]) -- then get the timer details 
        if playerAccount and not isGuestAccount(playerAccount) then -- check if it's not a guest account 
            setAccountData(playeraccount,  "timer", tonumber(remaining)) -- save the timer using setAccountData 
            killTimer(timerTable[source]) -- kill the timer 
            timerTable[source] = nil 
        end 
    end 
end 
addEventHandler ("onPlayerQuit", root, save ) 
  
-- load the timer on player login 
function loadTimer (_,account) 
    local timer = getAccountData(account, "timer") -- get the timer using getAccountData 
    if (timer) then -- if the timer exist then 
        timerTable[source] = setTimer(function() end, tonumber(time), 1) -- start it again using setTimer 
    end 
end 
addEventHandler ("onPlayerLogin", root, loadTimer) 

Link to comment
Here is all what you need.
local timerTable = {} -- timer table 
  
-- save the time on player quit 
function save () 
        local playerAccount = getPlayerAccount ( source ) -- get the player account 
        if (isTimer(timerTable[source])) then -- if the timer exist then  
        local remaining = getTimerDetails(timerTable[source]) -- then get the timer details 
        if playerAccount and not isGuestAccount(playerAccount) then -- check if it's not a guest account 
            setAccountData(playeraccount,  "timer", tonumber(remaining)) -- save the timer using setAccountData 
            killTimer(timerTable[source]) -- kill the timer 
            timerTable[source] = nil 
        end 
    end 
end 
addEventHandler ("onPlayerQuit", root, save ) 
  
-- load the timer on player login 
function loadTimer (_,account) 
    local timer = getAccountData(account, "timer") -- get the timer using getAccountData 
    if (timer) then -- if the timer exist then 
        timerTable[source] = setTimer(function() end, tonumber(time), 1) -- start it again using setTimer 
    end 
end 
addEventHandler ("onPlayerLogin", root, loadTimer) 

your code won't work

Link to comment

should i delete the current timer table i have here

and change the current timer[source] names? to timerTable[source] as wel?

local timer = {} 
  
function ex(source) 
if timer[source] and isTimer(timer[source]) then 
return end 
     timer[source] = setTimer(function() 
     timer[source] = nil 
     outputChatBox("blablablabla", source) 
     end, 3600*500, 1) 
     end 
addCommandHandler ( "ex", ex ) 

Link to comment

Use my code :

local timerTable = {} -- timer table 
  
-- save the time on player quit 
function save () 
        local playerAccount = getPlayerAccount ( source ) -- get the player account 
        if (isTimer(timerTable[source])) then -- if the timer exist then 
        local remaining = getTimerDetails(timerTable[source]) -- then get the timer details 
        if playerAccount and not isGuestAccount(playerAccount) then -- check if it's not a guest account 
            setAccountData(playeraccount,  "timer", tonumber(remaining)) -- save the timer using setAccountData 
            killTimer(timerTable[source]) -- kill the timer 
            timerTable[source] = nil 
        end 
    end 
end 
addEventHandler ("onPlayerQuit", root, save ) 
  
-- load the timer on player login 
function loadTimer (_,account) 
    local timer = getAccountData(account, "timer") -- get the timer using getAccountData 
    if (timer) then -- if the timer exist then 
        timerTable[source] = setTimer(function() end, tonumber(timer), 1) -- start it again using setTimer 
    end 
end 
addEventHandler ("onPlayerLogin", root, loadTimer) 

ofc it won't work you need to put this somewhere in your code:

-- Example:

if not isTimer(timerTable[thePlayer]) then  
    timerTable[thePlayer] = setTimer(function() end, 900000, 1 )  
end  

Link to comment
  
function ex(source) 
if timerTable[source] and isTimer(timerTable[source]) then 
return end 
     if not isTimer(timerTable[thePlayer]) then 
    timerTable[thePlayer] = setTimer(function() 
    --blabla-- 
     end, 900000, 1 ) 
     end 
addCommandHandler ( "ex", ex ) 

Edited by Guest
Link to comment

Should be like this:

function ex(player) 
    -- if the timer exist then  
    if isTimer(timerTable[player]) then  
        outputChatBox( "Please wait!", player, 255, 0, 0)  
        return  
    end 
    timerTable[player] = setTimer(function() end, 900000, 1 ) 
end 
addCommandHandler ("ex",ex) 

Link to comment
SOLVED,

Thanks to Tox and Walid.

now i know how to save a timer ^-^

Simply to optimize your code you could use the player serial to save the timer without using setAccountData() , getAccountData() , onPlayerQuit bla bla bla > waste of time.

timerTable[getPlayerSerial(player)] = setTimer(function() end, 900000, 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...