So basically, I am trying to get data from my datastore, but I am really lost and confused. The part that makes me lost though is how to save data and get data from objects added in a running server instead of in studio. For context I am making a system that will add an intvalue from clicking a button and releasing an item in a server.
Code:
local DataStore = game:GetService("DataStoreService")
game.Players.PlayerAdded:Connect(function(plr)
local ds = DataStore:GetDataStore("Data")
local inventory = Instance.new("Folder", plr)
inventory.Name = "inventory"
local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"
local Money = Instance.new("IntValue",leaderstats)
Money.Name = "R$"
Money.Value = 500
local item1 = Instance.new("IntValue", inventory)
item1.Value = 0
item1.Name = "Bow"
local item2 = Instance.new("IntValue", inventory)
item2.Value = 0
item2.Name = "Elton John - Classic Hair"
local item3 = Instance.new("IntValue", inventory)
item3.Value = 0
item3.Name = "Mask"
local item4 = Instance.new("IntValue", inventory)
item4.Value = 0
item4.Name = "Tablet"
for i,plr in pairs(game.Players:GetPlayers()) do
for i2,v in pairs(plr.inventory:GetChildren()) do
if v:IsA("IntValue") then
local ds = DataStore:GetDataStore(v.Name.."Data")
v.Changed:Connect(function(new)
print("saved")
end)
end
end
end
plr.inventory.ChildAdded:Connect(function(child)
wait(2)
if child.Value == 1 then
local clone = script.Parent.InventoryItem:Clone()
clone.Parent = script.Parent
clone.Name = child.Name
clone.Visible = true
clone.ItemName.Text = child.Name
clone.Image = "rbxthumb://type=Asset&w=420&h=420&id="..script.Parent.Parent.Parent.CatalogFrame.CatalogItems:FindFirstChild(child.Name).ItemId.Value
end
child.Changed:Connect(function(new)
if new == 1 then
local clone2 = script.Parent.InventoryItem:Clone()
clone2.Parent = script.Parent
clone2.Name = child.Name
clone2.Visible = true
clone2.ItemName.Text = child.Name
clone2.Image = "rbxthumb://type=Asset&w=420&h=420&id="..script.Parent.Parent.Parent.CatalogFrame.CatalogItems:FindFirstChild(child.Name).ItemId.Value
ds:SetAsync(child.Name.."Data", child.Value, plr.UserId)
end
end)
end)
end)```