Jump to content

top time


Wisin

Recommended Posts

hi everyone, im trying to make a script that will get the 2 players who have more hours, but i have no clue how to make it, if anyone can give me a start.

addCommandHandler("toptime", 
function (thePlayer) 
outputChatBox ("Top time online", thePlayer, 153, 51, 255, true) 
for k,v in ipairs(players) do 
local account = getPlayerAccount(v) 
local accountname = getAccountName(account) 
local hours = executeSQLSelect ( "rol_time", "H","Player = '" .. accountname .. "'") 
local h = hours[1]["H"] 
local nick = getPlayerName(v) 
outputChatBox(nick.." ".. tostring(h) .." hours", getRootElement (), 255, 255, 0, false) 
    end 
end) 

Link to comment
executeSQLQuery("SELECT * FROM your_table ORDER BY play_time LIMIT 2") 

replace * if you need to select only columns you need (like: playername)

replace your_table with table name

replace play_time with play time column name

edit: i see you want to get two best times from online players..

something like that

  
function getTop2() 
  local top = { 
    "times" = { 0, 0 }, 
    "accs" = { nil, nil } 
  } 
  for key,val in ipairs(getElementsByType("player") do 
    local time = replace_this_with_your_query 
    if tonumber(time) > top['times'][1] then 
      top['times'][2] = top['times'][1] 
      top['accs'][2] = top['accs'][1] 
      top['times'][1] = tonumber(time) 
      top['accs'][1] = getPlayerAccount(val) 
    elseif tonumber(time) > top['times'][1] then 
      top['times'][2] = tonumber(time) 
      top['accs'][2] = getPlayerAccount(val) 
    end 
  end 
end 
  

Link to comment

dont work this code,

  
function getTop2() 
outputChatBox ("Top time online", getRootElement(), 153, 51, 255, true) 
  local top = { 
    "times" = { 0, 0 }, 
    "accs" = { nil, nil } 
  } 
  for key,val in ipairs(getElementsByType("player") do 
  local account = getPlayerAccount(val) 
  local accountname = getAccountName(account) 
    local time = executeSQLSelect ( "rol_time", "H","Player = '" .. accountname .. "'") 
    if tonumber(time) > top['times'][1] then 
      top['times'][2] = top['times'][1] 
      top['accs'][2] = top['accs'][1] 
      top['times'][1] = tonumber(time) 
      top['accs'][1] = getPlayerAccount(val) 
      local nick = getPlayerName(val) 
    elseif tonumber(time) > top['times'][1] then 
      top['times'][2] = tonumber(time) 
      top['accs'][2] = getPlayerAccount(val) 
      local nick = getPlayerName(val) 
    end 
      outputChatBox(nick.." ".. tostring(h) .." hours", getRootElement (), 255, 255, 0, false) 
  end 
end 
addCommandHandler("toptime",getTop2) 

error: 498: '}' expected (to close '{' at line 497) near '='

Link to comment

omg guys, you cant read?

syntax errors are perfectly easy to spot and fix, as you can read WHAT is wrong, and WHERE (and most times: HOW TO FIX)..

probably the problem is here:

  
  local top = { 
    "times" = { 0, 0 }, 
    "accs" = { nil, nil } 
  } 
  

  
  local top = { 
    ["times"] = { 0, 0 }, 
    ["accs"] = { nil, nil } 
  } 
  

Link to comment
there isnt 498 line.

please give correct line, show us the code from 495-500 please.

i cant post that part its not from this function, that worked varez but now when i put /toptime says 1 errors

attempt to compare number with nil at line: if tonumber(time) > top['times'][1] then

Link to comment

ok i fixed that but now works but my friend have 3 hours and me 2 and he is down, why?

  
function getTop2() 
outputChatBox ("Top time online", getRootElement(), 153, 51, 255, true) 
  local top = { 
    ["times"] = { 0, 0 }, 
    ["accs"] = { nil, nil } 
  } 
  for key,val in ipairs(getElementsByType("player")) do 
  local account = getPlayerAccount(val) 
  local accountname = getAccountName(account) 
    local time = executeSQLSelect ( "rol_time", "H","Player = '" .. accountname .. "'") 
    local h = time[1]["H"] 
    if tonumber(h) > top['times'][1] then 
      top['times'][2] = top['times'][1] 
      top['accs'][2] = top['accs'][1] 
      top['times'][1] = tonumber(h) 
      top['accs'][1] = getPlayerAccount(val) 
      local nick = getPlayerName(val) 
      outputChatBox(nick.." ".. tostring(h) .." hours", getRootElement (), 255, 255, 0, false) 
    elseif tonumber(h) > top['times'][1] then 
      top['times'][2] = tonumber(h) 
      top['accs'][2] = getPlayerAccount(val) 
      local nick = getPlayerName(val) 
      outputChatBox(nick.." ".. tostring(h) .." hours", getRootElement (), 255, 255, 0, false) 
    end 
  end 
end 
addCommandHandler("toptime",getTop2) 

Link to comment

because you didnt try to understand how its working. these outputChatBox'es are inside the loop!

after loop (just before function end) you can access these values:

top["times"][1] - time of player on top 1

top["accs"][1] - account element of player on top 1

same for 2

if you need players elements in table - simply add it like the "accs" values are assigned. with a little trying you can do it

Link to comment
ok i fixed that but now works but my friend have 3 hours and me 2 and he is down, why?
  
function getTop2() 
outputChatBox ("Top time online", getRootElement(), 153, 51, 255, true) 
  local top = { 
    ["times"] = { 0, 0 }, 
    ["accs"] = { nil, nil } 
  } 
  for key,val in ipairs(getElementsByType("player")) do 
  local account = getPlayerAccount(val) 
  local accountname = getAccountName(account) 
    local time = executeSQLSelect ( "rol_time", "H","Player = '" .. accountname .. "'") 
    local h = time[1]["H"] 
    if tonumber(h) > top['times'][1] then 
      top['accs'][1] = getPlayerAccount(val) 
      local nick = getPlayerName(val) 
      outputChatBox(nick.." ".. tostring(h) .." hours", getRootElement (), 255, 255, 0, false) 
      top['times'][1] = tonumber(h) 
      top['accs'][1] = getPlayerAccount(val) 
    elseif tonumber(h) > top['times'][1] then 
      local nick = getPlayerName(val) 
      outputChatBox(nick.." ".. tostring(h) .." hours", getRootElement (), 255, 255, 0, false) 
      top['times'][1] = top['times'][1] 
      top['accs'][1] = top['accs'][1] 
      top['times'][1] = tonumber(h) 
    end 
  end 
end 
addCommandHandler("toptime",getTop2) 

i think he means this

Link to comment
because you didnt try to understand how its working. these outputChatBox'es are inside the loop!

after loop (just before function end) you can access these values:

top["times"][1] - time of player on top 1

top["accs"][1] - account element of player on top 1

same for 2

if you need players elements in table - simply add it like the "accs" values are assigned. with a little trying you can do it

i dont understand what you mean :oops:

Link to comment

oh my .....

  
function getTop2() 
outputChatBox ("Top time online", getRootElement(), 153, 51, 255, true) 
  local top = { 
    ["times"] = { 0, 0 }, 
    ["accs"] = { nil, nil } 
    ["pls"] = { nil, nil } 
  } 
  for key,val in ipairs(getElementsByType("player")) do 
  local account = getPlayerAccount(val) 
  local accountname = getAccountName(account) 
    local time = executeSQLSelect ( "rol_time", "H","Player = '" .. accountname .. "'") 
    local h = time[1]["H"] 
    if tonumber(h) > top['times'][1] then 
      top['times'][2] = top['times'][1] 
      top['accs'][2] = top['accs'][1] 
      top['pls'][2] = top['pls'][1] 
      top['times'][1] = tonumber(h) 
      top['accs'][1] = getPlayerAccount(val) 
      top['pls'][1] = val 
    elseif tonumber(h) > top['times'][1] then 
      top['times'][2] = tonumber(h) 
      top['accs'][2] = getPlayerAccount(val) 
      top['pls'][2] = val 
    end 
  end 
  outputChatBox("top1: player: "..getPlayerName(top['pls'][1])..", account: "..getAccountName(top['accs'][1])..", time: ".. top['times'][1]) 
  outputChatBox("top2: player: "..getPlayerName(top['pls'][2])..", account: "..getAccountName(top['accs'][2])..", time: ".. top['times'][2]) 
end 
addCommandHandler("toptime",getTop2) 

and i hope before copying-pasting it you will try to read it, until you understand it, as i give you ready solution ..

Link to comment
executeSQLQuery("SELECT * FROM your_table ORDER BY play_time LIMIT 2") 

replace * if you need to select only columns you need (like: playername)

replace your_table with table name

replace play_time with play time column name

addCommandHandler ("Top", 
function(player,command) 
top = executeSQLQuery("SELECT Name, Points FROM Money ORDER BY Points LIMIT 5") 
for i, playerdata in ipairs(top) do 
             outputChatBox(playerdata.Name .. ": " .. playerdata.Points, player) 
end 
end) 

I'm not so good at this SQL things so i wonder what is wrong? they are no errors

Link to comment

no is not, i have added "if account then" now i go test.

Edit: not working :( it says error in line: local h = time[1]["H"] attempt to index field '?' (a nil value)

  
function getTop3() 
outputChatBox ("Top time online", getRootElement(), 153, 51, 255, true) 
  local top = { 
    ["times"] = { 0, 0, 0 }, 
    ["accs"] = { nil, nil, nil }, 
    ["pls"] = { nil, nil, nil }, 
  } 
  for key,val in ipairs(getElementsByType("player")) do 
  local account = getPlayerAccount(val) 
  if account then   
  local accountname = getAccountName(account) 
    local time = executeSQLSelect ( "rol_time", "H","Player = '" .. accountname .. "'") 
    local h = time[1]["H"] 
    if tonumber(h) > top['times'][1] then 
      top['times'][2] = top['times'][1] 
      top['accs'][2] = top['accs'][1] 
      top['pls'][2] = top['pls'][1] 
      top['times'][1] = tonumber(h) 
      top['accs'][1] = getPlayerAccount(val) 
      top['pls'][1] = val 
    elseif tonumber(h) > top['times'][1] then 
      top['times'][2] = tonumber(h) 
      top['accs'][2] = getPlayerAccount(val) 
      top['pls'][2] = val   
      end 
    end 
  end 
  outputChatBox("top1: player: "..getPlayerName(top['pls'][1])..", hours: ".. top['times'][1],getRootElement(),255,0,0) 
  outputChatBox("top2: player: "..getPlayerName(top['pls'][2])..", hours: ".. top['times'][2],getRootElement(),0,255,0) 
  outputChatBox("top3: player: "..getPlayerName(top['pls'][3])..", hours: ".. top['times'][3],getRootElement(),0,0,255) 
end 
addCommandHandler("toptime",getTop3) 

Link to comment
function getTop3() 
outputChatBox ("Top time online", getRootElement(), 153, 51, 255, true) 
  local top = { 
    ["times"] = { 0, 0, 0 }, 
    ["accs"] = { nil, nil, nil }, 
    ["pls"] = { nil, nil, nil }, 
  } 
  for key,val in ipairs(getElementsByType("player")) do 
  local account = getPlayerAccount(val) 
  if account then   
  local accountname = getAccountName(account) 
    local time = executeSQLSelect ( "rol_time", "H","Player = '" .. accountname .. "'") 
    if time~=nil and #time>0 then 
    if tonumber(h) > top['times'][1] then 
      top['times'][2] = top['times'][1] 
      top['accs'][2] = top['accs'][1] 
      top['pls'][2] = top['pls'][1] 
      top['times'][1] = tonumber(h) 
      top['accs'][1] = getPlayerAccount(val) 
      top['pls'][1] = val 
    elseif tonumber(h) > top['times'][1] then 
      top['times'][2] = tonumber(h) 
      top['accs'][2] = getPlayerAccount(val) 
      top['pls'][2] = val   
        end 
      end 
    end 
  end 
  outputChatBox("top1: player: "..getPlayerName(top['pls'][1])..", hours: ".. top['times'][1],getRootElement(),255,0,0) 
  outputChatBox("top2: player: "..getPlayerName(top['pls'][2])..", hours: ".. top['times'][2],getRootElement(),0,255,0) 
  outputChatBox("top3: player: "..getPlayerName(top['pls'][3])..", hours: ".. top['times'][3],getRootElement(),0,0,255) 
end 
addCommandHandler("toptime",getTop3) 

thats it

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