Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created January 29, 2020 00:54
Show Gist options
  • Save howmanysmall/d08193cbd33b0c0c38e96c00d981f1cb to your computer and use it in GitHub Desktop.
Save howmanysmall/d08193cbd33b0c0c38e96c00d981f1cb to your computer and use it in GitHub Desktop.
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local Resources = require(ReplicatedStorage.Resources)
local Cryo = Resources:LoadLibrary("Cryo")
local DataStore2 = Resources:LoadLibrary("DataStore2")
local ItemsDictionary = Resources:LoadLibrary("ItemsDictionary")
local t = Resources:LoadLibrary("t")
local repr = require(3148021300)
DataStore2.Combine("MainData", "Coins", "Inventory")
--local REPR_SETTINGS = {
-- pretty = true,
--}
local playerInstance = t.instanceIsA("Player")
local function beforeInitialGet(serialized)
local deserialized = {}
for _, id in pairs(serialized) do
local itemName = ItemsDictionary[id]
deserialized[itemName] = true
end
return deserialized
end
local function beforeSave(deserialized)
local serialized = {}
for itemName in pairs(deserialized) do
for itemId, name in ipairs(ItemsDictionary) do
if name == itemName then
table.insert(serialized, itemId)
end
end
end
return serialized
end
local function characterAddedFenv(player)
return function(character)
CollectionService:AddTag(character, "ConnectionCreated")
local humanoid = character:WaitForChild("Humanoid")
local connection
connection = humanoid.Died:Connect(function()
connection:Disconnect()
local creator = humanoid:FindFirstChild("creator")
if creator and creator.Value and creator.Value.Parent then
local coinsStore = DataStore2("Coins", creator.Value)
coinsStore:Increment(5)
end
end)
end
end
local function playerAdded(player)
if not player:FindFirstChild("leaderstats") then
local coinsStore = DataStore2("Coins", player)
local inventoryStore = DataStore2("Inventory", player)
inventoryStore:BeforeInitialGet(beforeInitialGet)
inventoryStore:BeforeSave(beforeSave)
local leaderboard = Instance.new("Folder")
leaderboard.Name = "leaderstats"
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = coinsStore:Get(0)
coins.Parent = leaderboard
local inventory = Instance.new("Folder")
inventory.Name = "Inventory"
local currentInventory = inventoryStore:GetTable({})
for item in pairs(currentInventory) do
local object = Instance.new("BoolValue")
object.Name = item
object.Value = true
object.Parent = inventory
local success, tool = pcall(Resources.GetTool, Resources, item)
if success then
if not player.Backpack:FindFirstChild(item) then
tool:Clone().Parent = player.Backpack
end
if not player.StarterGear:FindFirstChild(item) then
tool:Clone().Parent = player.StarterGear
end
else
warn(string.format("couldn't find tool %s inside of Resources.Tools.", item))
end
end
inventory.Parent = leaderboard
leaderboard.Parent = player
coinsStore:OnUpdate(function(newCoins)
assert(t.integer(newCoins))
coins.Value = newCoins
end)
inventoryStore:OnUpdate(function(newInventory)
assert(t.table(newInventory))
for item in pairs(newInventory) do
if not inventory:FindFirstChild(item) then
local object = Instance.new("BoolValue")
object.Name = item
object.Value = true
object.Parent = inventory
local success, tool = pcall(Resources.GetTool, Resources, item)
if success then
if not player.Backpack:FindFirstChild(item) then
tool:Clone().Parent = player.Backpack
end
if not player.StarterGear:FindFirstChild(item) then
tool:Clone().Parent = player.StarterGear
end
else
warn(string.format("couldn't find tool %s inside of Resources.Tools.", item))
end
end
end
end)
local characterAdded = characterAddedFenv(player)
player.CharacterAdded:Connect(characterAdded)
if player.Character and not CollectionService:HasTag(player.Character, "ConnectionCreated") then
characterAdded(player.Character)
end
end
end
Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment