Hello! I am working on a game but I have encountered a problem. Im making an inventory gui that stores data such as the Name and amount. This inventory doesnt relate to the actual roblox inventory and is just UI based. Im trying to program it so that it saves and loads everything again. When a player joins they spawn with a lot of parts in their inventory. But for some reason it randomly clones
local datastore = game:GetService(“DataStoreService”)
local SaveData = datastore:GetDataStore(“SaveData”)
local inventory = datastore:GetDataStore(“inventory2”)
game.Players.PlayerAdded:Connect(function(player)
player:WaitForChild(“PlayerGui”)
player.PlayerGui:WaitForChild(“Inventory”)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
local lr = {
"Wall 1 Wall",
"Cup 1 Cup"
}
local data = SaveData:GetAsync(player.UserId.."-Cash")
local base = player.PlayerGui.Inventory.Main:WaitForChild("Temp")
local search = inventory:GetAsync(player.UserId.."-inv")
print(search)
if not search then
search = lr
end
for i, v in pairs(search) do
local split = string.split(v, " ")
local clone = base:Clone()
clone.Name = "Frame"
clone.Called.Value = split[1]
clone.number.Value = tonumber(split[2])
clone.Amount.Text = split[2]
clone.Header.Text = split[1]
clone.Parent = player.PlayerGui.Inventory.Main
end
if data then
print("Saved data!")
else
warn("Error: Can't find Cash data for the player.")
end
Cash.Value = data or 0
end)
game.Players.PlayerRemoving:Connect(function(player)
local save = {}
for i, v in pairs(player:WaitForChild(“PlayerGui”).Inventory.Main:GetChildren()) do
if v:IsA(“Frame”) and v.Name == “Frame” then – Filter only the “Frame” instances
local itemName = v.Called.Value
local quantity = v.number.Value
table.insert(save, itemName…" “…quantity…” "…v.Header.Text)
print(save)
end
end
local inventoryData = table.concat(save, “;”) – Join the items with a separator
print(“Inventory to save:”, inventoryData)
local s, e = pcall(function()
print("Made it to pcall")
inventory:SetAsync(player.UserId.."-inv", inventoryData)
print(inventory:GetAsync(player.UserId.."-inv"))
end)
if s then
print("Saved inventory!")
else
warn("Error: Failed to save inventory data.")
warn(e)
end
local s, e = pcall(function()
SaveData:SetAsync(player.UserId.."-Cash", player.leaderstats.Cash.Value)
end)
if s then
print("Data saved!")
else
warn("Error: Failed to save Cash data.")
warn(e)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local save = {}
for i, v in pairs(player.PlayerGui.Inventory.Main:GetChildren()) do
if v:IsA(“Frame”) and v.Name == “Frame” then – Filter only the “Frame” instances
local itemName = v.Called.Value
local quantity = v.number.Value
table.insert(save, itemName…" “…quantity…” "…v:FindFirstChildOfClass(“TextButton”).Name)
print(save)
end
end – Join the items with a separator
print(“Inventory to save:”, save)
local s, e = pcall(function()
print("")
inventory:SetAsync(player.UserId.."-inv", save)
print(inventory:GetAsync(player.UserId.."-inv"))
end)
if s then
print("Saved inventory!")
else
warn("Error: Failed to save inventory data.")
warn(e)
end
local s, e = pcall(function()
SaveData:SetAsync(player.UserId.."-Cash", player.leaderstats.Cash.Value)
end)
if s then
print("Data saved!")
else
warn("Error: Failed to save Cash data.")
warn(e)
end
end)