Jump to content

Need a genius' help


Lukkaz

Recommended Posts

Hi everyone I'm sorry I have been posting a lot of questions lately mostly things with stupid quick fixes that I over look but this will be my final question, I have created a jail script where time is saved in mySQL, however how can I get the time in mySQL to count down until the persons time is up?

  
function update(player) 
local characterID = exports.players:getCharacterID( player ) 
    if characterID then 
    local result = exports.sql:query_assoc( "SELECT jailTime FROM characters WHERE characterID ="..characterID ) 
             for key, value in ipairs( result ) do 
            local time = value.jailTime 
        if (time>=1) then 
            exports.sql:query_insertid( "INSERT INTO characters (jailTime) VALUES (" ..time .. ")") 
        end 
    end 
end 
end 
  
  
function onResourceStart(thisResource) 
  setTimer ( update, 600, 0 ) 
end 
addEventHandler ( "onResourceStart", getResourceRootElement(getThisResource()), onResourceStart ) 
  
local root = getRootElement() 
  
addEventHandler("onPlayerLogin", root, 
  function() 
local characterID = exports.players:getCharacterID( source ) 
    if characterID then 
        local time = exports.sql:query_assoc( "SELECT jailTime FROM characters WHERE characterID = "..characterID ) 
            if ( time ) and type( time ) == "number" then 
          if ( time>=1 ) then    
            local time = tonumber(time) 
            setTimer(timers, time*60000, 1, source) 
  
          end 
          else outputDebugString("time isn;t a number?") 
      end 
      outputDebugString("cant find characterid") 
  end 
 end 
) 
  
addCommandHandler( "jailtime", 
function ( player, commandName ) 
    if exports.players:isLoggedIn( player ) then 
    local characterID = exports.players:getCharacterID( player ) 
    if characterID then 
     local result = exports.sql:query_assoc( "SELECT jailTime FROM characters WHERE characterID = "..characterID ) 
            for key, value in ipairs( result ) do 
            local time = value.jailTime 
            if ( time >=1 ) then     
            outputChatBox ( "Your jailtime is "..time.." minute(s).", player, 0, 255, 0 ) 
            setTimer ( update, 600, 0, player ) 
            else  
            outputDebugString("its not working it cant be compared") 
        end 
    end 
    else 
end 
end 
end 
) 
  
function timers(other) 
local characterID = exports.players:getCharacterID( other ) 
if characterID then 
     local result = exports.sql:query_assoc( "SELECT jailTime FROM characters WHERE characterID = "..characterID ) 
            for key, value in ipairs( result ) do 
            local time = value.jailTime 
            if ( time == 0 ) then 
            setElementPosition(other, 2290, 2423.4, 10.8 ) 
            setElementInterior (other, 0 ) 
            outputChatBox( "Your time has been served, stay out of trouble.", other, 0, 255, 0 )  
            setElementDimension(other, 0 ) 
    end 
end 
end 
end 
  
  

What's the best way to do this I will do whatever for the person who can help me out me and my team are stumped

Edited by Guest
Link to comment

okay bare with me I am unable to test atm but

  
local root = getRootElement() 
  
addEventHandler("onPlayerLogin", root, 
  function() 
local characterID = exports.players:getCharacterID( source ) 
    if characterID then 
        local time = exports.sql:query_assoc( "SELECT jailTime FROM characters WHERE characterID = "..characterID ) 
            if ( time ) and type( time ) == "number" then 
          if ( time>=1 ) then    
            local time = tonumber(time) 
            setTimer(timers, time*60000, 1, source)     ----I set timer for the unjail script but how does it go into mysql 
  
          end 
          else outputDebugString("time isn;t a number?") 
      end 
      outputDebugString("cant find characterid") 
  end 
 end 
) 
  
addCommandHandler( "jailtime", 
function ( player, commandName ) 
    if exports.players:isLoggedIn( player ) then 
    local characterID = exports.players:getCharacterID( player ) 
    if characterID then 
     local result = exports.sql:query_assoc( "SELECT jailTime FROM characters WHERE characterID = "..characterID ) 
            for key, value in ipairs( result ) do 
            local time = value.jailTime 
            if ( time >=1 ) then     
                        getTimerDetails(timers)              ---------get the details of the unjail script 
            outputChatBox ( "Your jailtime is "..time.." minute(s).", player, 0, 255, 0 ) ----but how do I log it into mysql? 
             
            else  
        end 
    end 
    else 
end 
end 
end 
) 
  
  
function timers(other) -----unjail script 
local characterID = exports.players:getCharacterID( other ) 
if characterID then 
     local result = exports.sql:query_assoc( "SELECT jailTime FROM characters WHERE characterID = "..characterID ) 
            for key, value in ipairs( result ) do 
            local time = value.jailTime 
            if ( time == 0 ) then -----if the timer countde down to 0 then release the player 
            setElementPosition(other, 2290, 2423.4, 10.8 ) 
            setElementInterior (other, 0 ) 
            outputChatBox( "Your time has been served, stay out of trouble.", other, 0, 255, 0 )  
            setElementDimension(other, 0 ) 
    end 
end 
end 
end 
  
  

Link to comment

I did not check your code very well but this is what you should do.

After jailing/logging in you put up a timer that runs a function each second. That function will do two things:

if the jailtime is higher then 0, you update in sql the time value minus 1 second and the timer continues

if the jailtime is lower then 1, you unjail the player and stop the timer

Link to comment

well that's what I originally had but when people get jailed they log out and log back in and when they do the timers lost so they will sit there until someone remembers to release them :lol:

just a lot easier if I can save it so when they disconnect it pauses the timer and when they come back it'll pick up where it left off

Link to comment

Dont clear the timer as they leave then.

Theres a few ways you can do it where the timer doesnt clear as they leave, using their name or ip.

I think the max jail time on my server is 60 seconds, that seems a very long time when ur stuck in a jail cell with nothing to do.

Link to comment

I try but the bug I'm trying to squash is it'll say you have 1 minute left but the timers frozen and won't count down that last minute if you could read my code and help me pick out what's sketchy and could cause it not to work I'd hiiiighly appreciate it I been at it for days rearranging how to save the time and then start it when they connect

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