Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --TimScript
- local dataStore = {}
- local dataStoreService = game:GetService("DataStoreService")
- local httpService = game:GetService("HttpService")
- local ssService = game:GetService("ServerScriptService")
- local runService = game:GetService("RunService")
- local Id = require(ssService.SetIds.Id)
- local leaderstatsStore = dataStoreService:GetDataStore("Leaderstats")
- local itemsStore = dataStoreService:GetDataStore("Items")
- local itemCache = {}
- local statsCache = {}
- local function tableFind(t,f)
- for _,v in pairs(t) do
- if v == f then
- return true
- end
- end
- return false
- end
- -------------------------------------------------
- function dataStore:SaveLeaderstats(plr)
- if runService:IsStudio() then return end
- local playerKey = "plr_" .. plr.UserId
- local toSave = dataStore:GetCachedStats(plr)
- if not toSave then
- toSave = {}
- for _,v in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
- table.insert(toSave,{v.Name,v.Value})
- end
- end
- local success, err = pcall(function()
- leaderstatsStore:SetAsync(playerKey,httpService:JSONEncode(toSave))
- end)
- if err then warn(err) end
- end
- function dataStore:GetLeaderstats(plr)
- if runService:IsStudio() then return end
- local playerKey = "plr_" .. plr.UserId
- local toGet
- local success, err = pcall(function()
- toGet = (leaderstatsStore:GetAsync(playerKey))
- end)
- if err then warn(err) end
- if success and toGet then
- return httpService:JSONDecode(toGet)
- end
- return {}
- end
- -------------------------------------------------
- function dataStore:CacheStats(plr)
- local playerCache = {}
- for _,v in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
- table.insert(playerCache,{v.Name,v.Value})
- end
- statsCache[plr.UserId] = playerCache
- print("Cached: "..plr.Name.."'s stats")
- end
- function dataStore:RemoveCachedStats(plr)
- statsCache[plr.UserId] = nil
- end
- function dataStore:GetCachedStats(plr)
- return statsCache[plr.UserId]
- end
- -------------------------------------------------
- function dataStore:SaveItems(plr,fast)
- if runService:IsStudio() then return end
- local playerKey = "plr_" .. plr.UserId
- local char
- if not fast then
- char = plr.Character or plr.CharacterAdded:Wait()
- end
- local toSave = {}
- if itemCache[plr.UserId] then
- local cache = itemCache[plr.UserId]
- for _,v in pairs(cache) do
- table.insert(toSave,v)
- end
- else
- for _,v in pairs(plr.Backpack:GetChildren()) do
- table.insert(toSave,v.Id.Value)
- end
- if char and char:FindFirstChildWhichIsA("Tool") then
- table.insert(toSave,char:FindFirstChildWhichIsA("Tool").Id.Value)
- end
- end
- local success, err = pcall(function()
- itemsStore:UpdateAsync(playerKey,function(old)
- local oldValue = httpService:JSONDecode(old or {})
- local hash = {}
- local newValue = {}
- for _,v in ipairs(oldValue) do
- if not hash[v] then
- table.insert(newValue,v)
- hash[v] = true
- end
- end
- return httpService:JSONEncode(newValue)
- end)
- print("Saved: "..plr.Name.."'s items")
- end)
- if err then warn(err) end
- end
- function dataStore:GetItems(plr)
- if runService:IsStudio() then return end
- local playerKey = "plr_" .. plr.UserId
- local ids
- local success, err = pcall(function()
- ids = itemsStore:GetAsync(playerKey)
- end)
- if err then warn(err) end
- if success and ids then
- local toGet = {}
- for _,v in pairs(httpService:JSONDecode(ids)) do
- if not tableFind(toGet, Id[tonumber(v)]) then
- table.insert(toGet, Id[tonumber(v)])
- end
- end
- return toGet
- end
- return {}
- end
- -------------------------------------------------
- function dataStore:CacheItems(plr,t,fast)
- local playerCache = {}
- local char
- if not fast then
- char = plr.Character or plr.CharacterAdded:Wait()
- end
- if t then
- for i,v in pairs(t) do
- table.insert(playerCache,v:WaitForChild("Id").Value)
- end
- else
- for _,v in pairs(plr.Backpack:GetChildren()) do
- table.insert(playerCache,v:WaitForChild("Id").Value)
- end
- if char and char:FindFirstChildWhichIsA("Tool") then
- table.insert(playerCache,char:FindFirstChildWhichIsA("Tool"):WaitForChild("Id").Value)
- end
- end
- itemCache[plr.UserId] = playerCache
- print("Cached: "..plr.Name.."'s items")
- end
- function dataStore:GetCachedItems(plr)
- local toGet = {}
- for i,v in pairs(itemCache[plr.UserId]) do
- table.insert(toGet,Id[v])
- end
- return toGet
- end
- return dataStore
Advertisement
Add Comment
Please, Sign In to add comment