/
Watch Backpacks.lua
100 lines (85 loc) · 3.17 KB
/
Watch Backpacks.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
if not game:IsLoaded() then
game.Loaded:Wait()
end
local Players = game:GetService("Players")
repeat wait()
until game.Players.LocalPlayer and game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:findFirstChild("Head") and game.Players.LocalPlayer.Character:findFirstChild("Humanoid")
local mouse = game.Players.LocalPlayer:GetMouse()
repeat wait() until mouse
if getgenv().playerLeavingConnection then
getgenv().playerLeavingConnection:Disconnect()
end
function getTime()
local HOUR = math.floor((tick() % 86400) / 3600)
local MINUTE = math.floor((tick() % 3600) / 60)
local SECOND = math.floor(tick() % 60)
local AP = HOUR > 11 and 'PM' or 'AM'
HOUR = (HOUR % 12 == 0 and 12 or HOUR % 12)
HOUR = HOUR < 10 and '0' .. HOUR or HOUR
MINUTE = MINUTE < 10 and '0' .. MINUTE or MINUTE
SECOND = SECOND < 10 and '0' .. SECOND or SECOND
return HOUR .. ':' .. MINUTE .. ' ' .. AP
end
function getBackpackContents(backpack)
local contents = {}
for _,item in pairs(backpack:GetChildren()) do
contents[item.Name] = item.Amount.Value
end
return contents
end
function getPlayerBackpackBeforeAndAfter(player)
local beginningBackpack = getBackpackContents(player.StarterGear)
local endingBackpack = getBackpackContents(player.Backpack)
local contents = {}
for itemName, startQuantity in pairs(beginningBackpack) do
contents[itemName] = {startQuantity = startQuantity; endQuantity = 0}
end
for itemName, endQuantity in pairs(endingBackpack) do
if contents[itemName] then
contents[itemName].endQuantity = endQuantity
else
contents[itemName] = {startQuantity = 0; endQuantity = endQuantity}
end
end
return contents
end
function getPlayerBackpackDiff(player)
local backpackBeforeAfter = getPlayerBackpackBeforeAndAfter(player)
local backpackDiffs = {}
for itemName, quantities in pairs(backpackBeforeAfter) do
if quantities.startQuantity ~= quantities.endQuantity then
backpackDiffs[itemName] = quantities
end
end
return backpackDiffs
end
local FOLDER_NAME = "Inventory Changes"
if not isfolder(FOLDER_NAME) then
makefolder(FOLDER_NAME)
end
function createFileTxt(player)
local filename = FOLDER_NAME .. "/" .. player.Name .. ".txt"
if not isfile(filename) then
writefile(filename, "")
end
return filename
end
function createFileCsv(player)
local filename = FOLDER_NAME .. "/" .. player.Name .. ".csv"
if not isfile(filename) then
writefile(filename, "player,date,item,start,end\n")
end
return filename
end
function logPlayerBackpackChanges(player)
local currentTime = getTime()
local dt = os.date("*t", os.time())
local txtFilename = createFileTxt(player)
local csvFilename = createFileCsv(player)
for itemName,quantities in pairs(getPlayerBackpackDiff(player)) do
local quantityChanged = quantities.endQuantity - quantities.startQuantity
appendfile(txtFilename, osTime .. " - " .. itemName .. " has changed " .. quantityChanged .. "\n")
appendfile(csvFilename, player.Name .. "," .. dt.month.."-"..dt.day.." "..currentTime .. "," .. itemName .. "," .. quantities.startQuantity .. "," .. quantities.endQuantity .. "\n")
end
end
getgenv().playerLeavingConnection = Players.PlayerRemoving:Connect(logPlayerBackpackChanges)