Datastore Cant Find PlayerGui

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.

try starterGui, might work since its the StarterGui for the Player


Didn’t work sadly.

You arent spelling it correctly,
StarterGui

Oh sorry my bad, anyways it still doesn’t work :frowning:

ok look,

You do this:

game.StarterGui
 -- or this:
game:GetService("StarterGui")

Not This:

Player.StarterGui

Plus why are you doing this?

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)

You are supposed to Get the Data first.

Try PlayerGui:WaitForChild(name), StarterGui is where UI is stored BEFORE it is parented into PlayerGui.

I’m not sure if you read the Topic:

Doesn’t that save the data for all players tho?

I was following this thread by the way.

To do DataStore, you have to first Get the Data (:GetAsync()), you are using SetAsync() with no data loaded, plus looking that the thread, you didnt add a function to it

Because you’re trying to access it from the server. This has many flaws. The user can just exploit and change the value. Secondly, you cannot access PlayerGui (I think) from the server as it is not replicated.

  1. That’s how DataStoreService works

  2. You can, the First Variable of PlayerAdded and PlayerRemoving is the player itself, from there you can access the Character, PlayerGui, Backpack, etc

I’m not talking about the player object, I’m talking about PlayerGui…

Yes, but you need access to the player to get access to the PlayerGui

What are you trying to say? I’m saying you shouldn’t be using PlayerGui from the server. You shouldn’t rely or trust the client to get data from it.

Instead, if your data isn’t anything critical, eg settings, you should just have the client send the data over to the server.

Thanks for the notice. This is the output after i added the success detections

Oh ok, the thread used playergui so that’s why I wrote that. But I’m still confused about the situation. The thread used serverstats as an example. And I remember serverstats is inside the player, and so is playergui. but for some reason serverstats can be found but now playergui.

For some reason when I changed the datastore name from “ItemStorage” to “MyDataStore” the script stopped giving me “Data has not been saved!”, but instead gives me this error.


So then I changed
player.PlayerGui.UI.StorageUI.Storage.Row1.Slot1.Slot.Value = data[1]
to
player.PlayerGui:WaitForChild("UI").StorageUI.Storage.Row1.Slot1.Slot.Value = data[1]
However the script then gave me this error:

I’m not sure what I did wrong.

it turns out that the reason the script kept giving me “unable to assign property value. string expected, got nil” is because the datastore name had a few data that overlapped the current data. I fixed it by changing the datastore’s name, however, I’m again getting the "PlayerGui is not a valid member of Player “Players.sailakedi”.