I am very new to Data Store and I am trying to store the Resources wood, stone, flax, pila into a data store so whenever the player leaves the game if the value was added or subtracted it would save upon his/hers next rejoin.
I need to save Resources.
Here is the code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ItemIndex = require(ReplicatedStorage:WaitForChild("ItemIndex")) local ApplyStandardIssue = require(script:WaitForChild("StandardIssue")) local PlayerInventoryFetch = ReplicatedStorage:WaitForChild("PlayerInventoryFetch") local PlayerInventoryUpdate = ReplicatedStorage:WaitForChild("PlayerInventoryUpdate") local PlayerInventoryEquipItem = ReplicatedStorage:WaitForChild("PlayerInventoryEquipItem")
local RESOURCE_MAX = { pila = 2 }
local PlayerInventories = {}
function AttachToCharacter(player, accessory) if accessory:IsA("Accessory") then player.Character.Humanoid:AddAccessory(accessory:Clone()) elseif accessory:IsA("Model") then local model = accessory:Clone()
local weld = Instance.new("ManualWeld")
weld.Part0 = model.Handle
weld.Part1 = player.Character[model.AttachPart.Value]
weld.Parent = model.Handle
model.Parent = player.Character
end
end
function FireInventoryUpdate(player, forceEquip) PlayerInventoryUpdate:FireClient(player, PlayerInventories[player.UserId], forceEquip) end
function AddToInventory(player, name, options) table.insert(PlayerInventories[player.UserId].Items, { Id = name, UniqueName = ItemIndex.Items[name].Name .. " " .. tick() .. " " .. math.random(1, 1000), Equipped = options.Equipped or false, StandardIssue = options.StandardIssue or false, FamiliarTemplate = options.FamiliarTemplate })
if not options.SkipUpdateEvent then
FireInventoryUpdate(player)
end
end
game.Players.PlayerAdded:Connect(function(player) local CharacterAppearanceConnection CharacterAppearanceConnection = player.CharacterAppearanceLoaded:Connect(function(character) CharacterAppearanceConnection:Disconnect() wait(0.6) if character:FindFirstChild("Shirt") then AddToInventory(player, "shirt_familiar", {Equipped=true, FamiliarTemplate=character.Shirt.ShirtTemplate}) end
if character:FindFirstChild("Pants") then
AddToInventory(player, "pants_familiar", {Equipped=true, FamiliarTemplate=character.Pants.PantsTemplate})
end
end)
wait(0.5)
if not PlayerInventories[player.UserId] then
PlayerInventories[player.UserId] = {
Resources = {wood = 1000, stone = 1000, flax = 1000, pila = 2},
Items = {}
}
AddToInventory(player, "weapon_gladius", {Equipped = true, SkipUpdateEvent = true})
AddToInventory(player, "weapon_pilum", {Equipped = true, SkipUpdateEvent = true})
AddToInventory(player, "weapon_bow", {Equipped = true, SkipUpdateEvent = true})
AddToInventory(player, "tool_shackles", {Equipped = false, SkipUpdateEvent = true})
AddToInventory(player, "tool_build", {Equipped = true, SkipUpdateEvent = true})
--AddToInventory(player, "tool_dolabra", {Equipped = true, SkipUpdateEvent = true})
ApplyStandardIssue(player, AddToInventory)
end
FireInventoryUpdate(player, true)
end)
script:WaitForChild("ModifyInventory").OnInvoke = function(player, resource, delta) if delta < 0 then if PlayerInventories[player.UserId].Resources[resource] < delta then return false end end
PlayerInventories[player.UserId].Resources[resource] = PlayerInventories[player.UserId].Resources[resource] + delta
FireInventoryUpdate(player)
return true
end
script:WaitForChild("InventoryManager").OnInvoke = function(player, action, data) if action == "AddResource" then local delta = data.Delta local resource = data.Resource
assert(delta ~= nil)
assert(resource ~= nil)
if delta < 0 then
if PlayerInventories[player.UserId].Resources[resource] < delta then
return false
end
end
PlayerInventories[player.UserId].Resources[resource] = PlayerInventories[player.UserId].Resources[resource] + delta
FireInventoryUpdate(player)
return true
elseif action == "HasResource" then
local amount = data.Amount
local resource = data.Resource
assert(amount ~= nil)
assert(resource ~= nil)
if PlayerInventories[player.UserId].Resources[resource] >= amount then
return true
end
return false
elseif action == "IsResourceFull" then
local resource = data.Resource
assert(resource ~= nil)
local amount = PlayerInventories[player.UserId].Resources[resource]
local max = RESOURCE_MAX[resource] or math.huge
if amount >= max then
return true, amount
end
return false, amount
end
end
PlayerInventoryFetch.OnServerInvoke = function(player) return PlayerInventories[player.UserId] end
function ExecuteEquipItem(player, uid, equip, skipUpdateEvent) local item
for _, invItem in pairs(PlayerInventories[player.UserId].Items) do
if invItem.UniqueName == uid then
item = invItem
end
end
if not item then return end
local itemInfo = ItemIndex.Items[item.Id]
if itemInfo.Type == "weapon" or itemInfo.Type == "tool" then
if equip then
-- if player.Team == game.Teams.Spectators then return end
itemInfo.Tool:Clone().Parent = player.Backpack
item.Equipped = true
else
local tool = player.Backpack:FindFirstChild(itemInfo.Tool.Name) or player.Character:FindFirstChild(itemInfo.Tool.Name)
if tool then
if tool.Parent:IsA("Model") then
tool.Parent = player.Backpack
wait(0.1)
end
tool:Destroy()
end
item.Equipped = false
end
elseif itemInfo.Type == "apparel" then
if equip then
AttachToCharacter(player, itemInfo.Accessory)
item.Equipped = true
if itemInfo.Accessory:FindFirstChild("RemoveHair") then
for _, child in pairs(player.Character:GetChildren()) do
if child:IsA("Accessory") and child:FindFirstChild("Handle") and (child.Handle:FindFirstChild("HatAttachment") or child.Handle:FindFirstChild("HairAttachment")) then
if not player.PlayerGui:FindFirstChild("RemovedHats") then
Instance.new("Folder", player.PlayerGui).Name = "RemovedHats"
end
child.Parent = player.PlayerGui.RemovedHats
end
end
end
else
local acc = player.Character:FindFirstChild(itemInfo.Accessory.Name)
if acc then
acc:Destroy()
end
item.Equipped = false
if not player.Character:FindFirstChild("RemoveHair", true) and player.PlayerGui:FindFirstChild("RemovedHats") then
for _, child in pairs(player.PlayerGui.RemovedHats:GetChildren()) do
player.Character.Humanoid:AddAccessory(child)
end
player.PlayerGui.RemovedHats:Destroy()
end
end
elseif itemInfo.Type == "shield" then
if equip then
local value = Instance.new("StringValue")
value.Name = "SHIELD"
value.Value = itemInfo.Model.Name
value.Parent = player.Character
item.Equipped = true
else
while player.Character:FindFirstChild("SHIELD") do
player.Character.SHIELD:Destroy()
end
while player.Character:FindFirstChild("ShieldBack") do
player.Character.ShieldBack:Destroy()
end
item.Equipped = false
end
elseif itemInfo.Type == "shirt" then
if equip then
local characterShirt = player.Character:FindFirstChild("Shirt") or Instance.new("Shirt", player.Character)
characterShirt.Name = "Shirt"
characterShirt.ShirtTemplate = item.Id == "shirt_familiar" and item.FamiliarTemplate or itemInfo.ShirtTemplate
item.Equipped = true
else
if player.Character:FindFirstChild("Shirt") then
player.Character.Shirt.ShirtTemplate = "rbxassetid://911246743"
end
item.Equipped = false
end
elseif itemInfo.Type == "pants" then
if equip then
local characterPants = player.Character:FindFirstChild("Pants") or Instance.new("Pants", player.Character)
characterPants.Name = "Pants"
characterPants.PantsTemplate = item.Id == "pants_familiar" and item.FamiliarTemplate or itemInfo.PantsTemplate
item.Equipped = true
else
if player.Character:FindFirstChild("Pants") then
player.Character.Pants.PantsTemplate = "rbxassetid://911247008"
end
item.Equipped = false
end
end
if not skipUpdateEvent then
FireInventoryUpdate(player)
end
return item.Equipped
end
PlayerInventoryEquipItem.OnServerInvoke = function(player, uid, equip, skipUpdateEvent) if type(uid) == "table" then for _, id in pairs(uid) do ExecuteEquipItem(player, id, equip, skipUpdateEvent) end else return ExecuteEquipItem(player, uid, equip, skipUpdateEvent) end
return true
end```

1 comment
What are you even doing