I’m trying to make a data store for a Storage GUI. When someone stores an item in the storage it will be saved permanently.
However, the datastore for some reason can’t find the PlayerGui so it fails to save the data when I leave the game.
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("ItemStorage")
local function saveData(player)
local SlotValuesTable = {
player.PlayerGui.UI.StorageUI.Storage.Row1.Slot1.Slot.Value, -- 1
player.PlayerGui.UI.StorageUI.Storage.Row1.Slot2.Slot.Value, -- 2
player.PlayerGui.UI.StorageUI.Storage.Row1.Slot3.Slot.Value, -- 3
player.PlayerGui.UI.StorageUI.Storage.Row1.Slot4.Slot.Value, -- 4
player.PlayerGui.UI.StorageUI.Storage.Row1.Slot5.Slot.Value, -- 5
player.PlayerGui.UI.StorageUI.Storage.Row2.Slot1.Slot.Value, -- 1
player.PlayerGui.UI.StorageUI.Storage.Row2.Slot2.Slot.Value, -- 2
player.PlayerGui.UI.StorageUI.Storage.Row2.Slot3.Slot.Value, -- 3
player.PlayerGui.UI.StorageUI.Storage.Row2.Slot4.Slot.Value, -- 4
player.PlayerGui.UI.StorageUI.Storage.Row2.Slot5.Slot.Value, -- 5
player.PlayerGui.UI.StorageUI.Storage.Row3.Slot1.Slot.Value, -- 1
player.PlayerGui.UI.StorageUI.Storage.Row3.Slot2.Slot.Value, -- 2
player.PlayerGui.UI.StorageUI.Storage.Row3.Slot3.Slot.Value, -- 3
player.PlayerGui.UI.StorageUI.Storage.Row3.Slot4.Slot.Value, -- 4
player.PlayerGui.UI.StorageUI.Storage.Row3.Slot5.Slot.Value, -- 5
player.PlayerGui.UI.StorageUI.Storage.Row4.Slot1.Slot.Value, -- 1
player.PlayerGui.UI.StorageUI.Storage.Row4.Slot2.Slot.Value, -- 2
player.PlayerGui.UI.StorageUI.Storage.Row4.Slot3.Slot.Value, -- 3
player.PlayerGui.UI.StorageUI.Storage.Row4.Slot4.Slot.Value, -- 4
player.PlayerGui.UI.StorageUI.Storage.Row4.Slot5.Slot.Value, -- 5
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, SlotValuesTable) -- Saves player id and the table
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
player.PlayerGui.UI.StorageUI.Storage.Row1.Slot1.Slot.Value = data[1]
player.PlayerGui.UI.StorageUI.Storage.Row1.Slot2.Slot.Value = data[2]
player.PlayerGui.UI.StorageUI.Storage.Row1.Slot3.Slot.Value = data[3]
player.PlayerGui.UI.StorageUI.Storage.Row1.Slot4.Slot.Value = data[4]
player.PlayerGui.UI.StorageUI.Storage.Row1.Slot5.Slot.Value = data[5]
player.PlayerGui.UI.StorageUI.Storage.Row2.Slot1.Slot.Value = data[6]
player.PlayerGui.UI.StorageUI.Storage.Row2.Slot2.Slot.Value = data[7]
player.PlayerGui.UI.StorageUI.Storage.Row2.Slot3.Slot.Value = data[8]
player.PlayerGui.UI.StorageUI.Storage.Row2.Slot4.Slot.Value = data[9]
player.PlayerGui.UI.StorageUI.Storage.Row2.Slot5.Slot.Value = data[10]
player.PlayerGui.UI.StorageUI.Storage.Row3.Slot1.Slot.Value = data[11]
player.PlayerGui.UI.StorageUI.Storage.Row3.Slot2.Slot.Value = data[12]
player.PlayerGui.UI.StorageUI.Storage.Row3.Slot3.Slot.Value = data[13]
player.PlayerGui.UI.StorageUI.Storage.Row3.Slot4.Slot.Value = data[14]
player.PlayerGui.UI.StorageUI.Storage.Row3.Slot5.Slot.Value = data[15]
player.PlayerGui.UI.StorageUI.Storage.Row4.Slot1.Slot.Value = data[16]
player.PlayerGui.UI.StorageUI.Storage.Row4.Slot2.Slot.Value = data[17]
player.PlayerGui.UI.StorageUI.Storage.Row4.Slot3.Slot.Value = data[18]
player.PlayerGui.UI.StorageUI.Storage.Row4.Slot4.Slot.Value = data[19]
player.PlayerGui.UI.StorageUI.Storage.Row4.Slot5.Slot.Value = data[20]
else
print("The player has no data!") -- The default will be set to 0
end
end)
game.Players.PlayerRemoving:Connect(function(player) -- player leaves
saveData(player) -- Save the data
end)
game:BindToClose(function() -- server shutdowns
for _, player in pairs(game.Players:GetPlayers()) do
saveData(player) -- Save the data
end
end)
(This is what the workspace looks like)
Any help is appreciated. Please tell me if you want more info.