Guest User

einsteinK's Visited Module

a guest
Jul 26th, 2015
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 KB | None | 0 0
  1.  
  2. --[[ einsteinK's Visited Module
  3.     Note: Should only be required by assetId
  4.         This assures no duplicates are running (which is nice)
  5.     Usage:
  6.         local Module = require(260280577) -- Load module
  7.         Module.OnJoin:connect(function(plr,tim)
  8.             local str = tim and math.floor((os.time()-tim)/60).."m ago"
  9.             print(plr,"has joined! Last time was",str or "never")
  10.         end)
  11.         Module.OnLeave:connect(function(plr,tim)
  12.             local str = tim and math.floor((os.time()-tim)/60).."m ago"
  13.             print(plr,"has left! He joined",str)
  14.         end)
  15.         local list = Module(nil,true)
  16.             -- nil --> Get as much entries as possible
  17.                 -- If you want to have only a certain amount of entries, give it a number
  18.                 -- It'll be ceiled to a multitude of 100. Eg: 56 --> 100, 101 --> 200
  19.                 -- (Since datastore pages can (and here will) only contain 100 entries)
  20.             -- true --> .name and .named will be available
  21.                 -- When enabling naming, it'll take a bit more of time (not measured)
  22.                 -- It's always sorted: At the top the most recent entries
  23.                 -- (An entry is added on join and leave)
  24.                 -- (There's only one entry per player: rejoin/leaving overwrites previous entry)
  25.         for k,v in pairs(list) do
  26.             print("Player",v.namem and v.name or "with no name","with userId",v.userId,
  27.                 "left a trace",math.floor((os.time()-v.timestamp)/60).."m ago")
  28.         end
  29.     Tip:
  30.         As owner, you can see who joined/left recently by running this command:
  31.         for k,v in pairs(require(260280577)(20,true)) do
  32.             print("Player",v.name,"(",v.userId,")",math.floor((os.time()-v.timestamp)/60).."m ago")
  33.         end
  34.        
  35.     Feel free to use this module in any script in any place, just give credits maybe?
  36.     (Credits as in, mention somewhere that this module is use. Supply the assetId/link)
  37. --]]
  38.  
  39. local DSS = game:GetService("DataStoreService")
  40. local ODS = DSS:GetOrderedDataStore("Visited")
  41. local Players = game:GetService("Players")
  42.  
  43. local OnLeave = Instance.new("BindableEvent")
  44. local OnJoin = Instance.new("BindableEvent")
  45.  
  46. local function Joined(plr)
  47.     if plr.userId <= 0 then return end local t
  48.     pcall(ODS.UpdateAsync,ODS,plr.userId,function(old)
  49.         t = old return os.time()
  50.     end) OnJoin:Fire(plr,t)
  51. end Players.PlayerAdded:connect(Joined)
  52. Players.PlayerRemoving:connect(function(plr)
  53.     if plr.userId <= 0 then return end local t
  54.     pcall(ODS.UpdateAsync,ODS,plr.userId,function(old)
  55.         t = old return os.time()
  56.     end) OnLeave:Fire(plr,t)
  57. end)
  58.  
  59. spawn(function()
  60.     for k,v in pairs(Players:GetPlayers()) do
  61.         pcall(coroutine.wrap(Joined),v)
  62.     end
  63. end)
  64.  
  65. local function Name(tab)
  66.     local s,e = pcall(Players.GetNameFromUserIdAsync,Players,tab.userId)
  67.     tab.name,tab.named = s and e or "["..tab.userId.."]",s
  68. end
  69. local threadcount = 0
  70. local function Thread(tab)
  71.     local c = coroutine.create(Name)
  72.     threadcount = threadcount + 1
  73.     coroutine.resume(c,tab)
  74.     repeat wait() until coroutine.status(c) == "dead"
  75.     threadcount = threadcount - 1
  76. end
  77.  
  78. return setmetatable({},{
  79.     __index = function(s,k)
  80.         if type(k) ~= "string" then return end
  81.         if k:lower() == "OnJoin" then
  82.             return OnJoin.Event
  83.         elseif k:lower() == "OnLeave" then
  84.             return OnLeave.Event
  85.         end
  86.     end;
  87.     __call = function(s,num,name) local res = {}
  88.         local pages = ODS:GetSortedAsync(false, 100)
  89.         while true do
  90.             local data = pages:GetCurrentPage()
  91.             for _,pair in pairs(data) do
  92.                 table.insert(res,{userId=pair.key,timestamp=pair.value})
  93.             end if num and #res >= num then break end
  94.             if pages.IsFinished then break end
  95.             pages:AdvanceToNextPageAsync()
  96.         end if not name then return res end
  97.         for k,v in pairs(res) do
  98.             while threadcount > 10 do wait() end
  99.             coroutine.wrap(Thread)(v)
  100.         end repeat wait() until threadcount == 0
  101.         return res
  102.     end; __metatable = "Locked";
  103.     __tostring = function()
  104.         return "[einsteinK's Visited Module]"
  105.     end;
  106. })
Add Comment
Please, Sign In to add comment