Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ einsteinK's Visited Module
- Note: Should only be required by assetId
- This assures no duplicates are running (which is nice)
- Usage:
- local Module = require(260280577) -- Load module
- Module.OnJoin:connect(function(plr,tim)
- local str = tim and math.floor((os.time()-tim)/60).."m ago"
- print(plr,"has joined! Last time was",str or "never")
- end)
- Module.OnLeave:connect(function(plr,tim)
- local str = tim and math.floor((os.time()-tim)/60).."m ago"
- print(plr,"has left! He joined",str)
- end)
- local list = Module(nil,true)
- -- nil --> Get as much entries as possible
- -- If you want to have only a certain amount of entries, give it a number
- -- It'll be ceiled to a multitude of 100. Eg: 56 --> 100, 101 --> 200
- -- (Since datastore pages can (and here will) only contain 100 entries)
- -- true --> .name and .named will be available
- -- When enabling naming, it'll take a bit more of time (not measured)
- -- It's always sorted: At the top the most recent entries
- -- (An entry is added on join and leave)
- -- (There's only one entry per player: rejoin/leaving overwrites previous entry)
- for k,v in pairs(list) do
- print("Player",v.namem and v.name or "with no name","with userId",v.userId,
- "left a trace",math.floor((os.time()-v.timestamp)/60).."m ago")
- end
- Tip:
- As owner, you can see who joined/left recently by running this command:
- for k,v in pairs(require(260280577)(20,true)) do
- print("Player",v.name,"(",v.userId,")",math.floor((os.time()-v.timestamp)/60).."m ago")
- end
- Feel free to use this module in any script in any place, just give credits maybe?
- (Credits as in, mention somewhere that this module is use. Supply the assetId/link)
- --]]
- local DSS = game:GetService("DataStoreService")
- local ODS = DSS:GetOrderedDataStore("Visited")
- local Players = game:GetService("Players")
- local OnLeave = Instance.new("BindableEvent")
- local OnJoin = Instance.new("BindableEvent")
- local function Joined(plr)
- if plr.userId <= 0 then return end local t
- pcall(ODS.UpdateAsync,ODS,plr.userId,function(old)
- t = old return os.time()
- end) OnJoin:Fire(plr,t)
- end Players.PlayerAdded:connect(Joined)
- Players.PlayerRemoving:connect(function(plr)
- if plr.userId <= 0 then return end local t
- pcall(ODS.UpdateAsync,ODS,plr.userId,function(old)
- t = old return os.time()
- end) OnLeave:Fire(plr,t)
- end)
- spawn(function()
- for k,v in pairs(Players:GetPlayers()) do
- pcall(coroutine.wrap(Joined),v)
- end
- end)
- local function Name(tab)
- local s,e = pcall(Players.GetNameFromUserIdAsync,Players,tab.userId)
- tab.name,tab.named = s and e or "["..tab.userId.."]",s
- end
- local threadcount = 0
- local function Thread(tab)
- local c = coroutine.create(Name)
- threadcount = threadcount + 1
- coroutine.resume(c,tab)
- repeat wait() until coroutine.status(c) == "dead"
- threadcount = threadcount - 1
- end
- return setmetatable({},{
- __index = function(s,k)
- if type(k) ~= "string" then return end
- if k:lower() == "OnJoin" then
- return OnJoin.Event
- elseif k:lower() == "OnLeave" then
- return OnLeave.Event
- end
- end;
- __call = function(s,num,name) local res = {}
- local pages = ODS:GetSortedAsync(false, 100)
- while true do
- local data = pages:GetCurrentPage()
- for _,pair in pairs(data) do
- table.insert(res,{userId=pair.key,timestamp=pair.value})
- end if num and #res >= num then break end
- if pages.IsFinished then break end
- pages:AdvanceToNextPageAsync()
- end if not name then return res end
- for k,v in pairs(res) do
- while threadcount > 10 do wait() end
- coroutine.wrap(Thread)(v)
- end repeat wait() until threadcount == 0
- return res
- end; __metatable = "Locked";
- __tostring = function()
- return "[einsteinK's Visited Module]"
- end;
- })
Add Comment
Please, Sign In to add comment