DataStore Not Working

Hi there! I’m currently trying to create a DataStore for saving the text on a gui (shop), but am having issues. I am experiencing no errors, so it’s hard to see what the issue is. Here is what I have so far:

ServerScript:

-- Getting some values for future usage
local DataStore = game:GetService("DataStoreService"):GetDataStore("ShopGUIData")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeGuiEvent = ReplicatedStorage:WaitForChild("Remotes").ChangeGuiEvent
local SaveGuiEvent = ReplicatedStorage:WaitForChild("Remotes").SaveGuiEvent
local FinishedSaveGuiEvent = ReplicatedStorage:WaitForChild("Remotes").FinishedSaveGuiEvent

local Players = game:GetService("Players")

-- Gets called when a player joins the game
Players.PlayerAdded:Connect(function(player)
	local success = pcall(function()				
		local data = DataStore:GetAsync(player.UserId)
		
		ChangeGuiEvent:FireClient(player, data)
		
		return data
	end)
end)

-- Gets called when a player leaves the game
Players.PlayerRemoving:Connect(function(player)
	FinishedSaveGuiEvent.OnServerEvent:Connect(function(player, datatable)
		-- Save the datatable to the datastore, so the next time the player joins, it will load their data
		local success, errorMessage = pcall(function()
			DataStore:SetAsync(player.UserId, datatable)
		end)

		-- If there is an error saving the data, print an error message
		if not success then
			print(errorMessage)
		end
	end)
	
	SaveGuiEvent:FireClient(player)
end)

GuiChangedListener:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeGuiEvent = ReplicatedStorage:WaitForChild("Remotes").ChangeGuiEvent
local FinishedChangeGuiEvent = ReplicatedStorage:WaitForChild("Remotes").FinishedChangeGuiEvent

local Player  = game:GetService("Players").LocalPlayer

ChangeGuiEvent.OnClientEvent:Connect(function(data)
	for i, child in pairs(Player.PlayerGui.backpackShop.MainShop.Info:GetChildren()) do
		if child:IsA('TextButton') then
			local buttonNumber = string.split(child.Name, "Buy")[2]
			
			child.Text = data[buttonNumber]
		end
	end	
end)

GuiSavedListener:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SaveGuiEvent = ReplicatedStorage:WaitForChild("Remotes").SaveGuiEvent
local FinishedSaveGuiEvent = ReplicatedStorage:WaitForChild("Remotes").FinishedSaveGuiEvent

local Player  = game:GetService("Players").LocalPlayer

SaveGuiEvent.OnClientEvent:Connect(function()
	local datatable = {}

	-- For each button in the shop, get it's text and it's index, and then store it to the table
	local size = #Player.PlayerGui.backpackShop.MainShop.Info:GetChildren()
	datatable = table.create(size, nil)

	for i, child in pairs(Player.PlayerGui.backpackShop.MainShop.Info:GetChildren()) do
		if child:IsA('TextButton') then
			local buttonText = child.Text
			local buttonNumber = string.split(child.Name, "Buy")[2]

			datatable[buttonNumber] = buttonText
		end
	end
	
	FinishedSaveGuiEvent:FireServer(datatable)
end)

Also, here’s my hierarchy if you need it:

Any help is appreciated, thanks!

Bump

(Character Limit) (CharacterLimit)

Place print statements in various parts of the code and see where it stops.

Apparently SaveGuiEvent is not getting called.

– Local script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SaveGuiEvent = ReplicatedStorage:WaitForChild("Remotes").SaveGuiEvent
local FinishedSaveGuiEvent = ReplicatedStorage:WaitForChild("Remotes").FinishedSaveGuiEvent

local Player  = game:GetService("Players").LocalPlayer

SaveGuiEvent.OnClientEvent:Connect(function()
	print("here")
	local datatable = {}

	-- For each button in the shop, get it's text and it's index, and then store it to the table
	local size = #Player.PlayerGui.backpackShop.MainShop.Info:GetChildren()
	datatable = table.create(size, nil)

	for i, child in pairs(Player.PlayerGui.backpackShop.MainShop.Info:GetChildren()) do
		if child:IsA('TextButton') then
			local buttonText = child.Text
			local buttonNumber = string.split(child.Name, "Buy")[2]

			datatable[buttonNumber] = buttonText
		end
	end
	
	
	
	FinishedSaveGuiEvent:FireServer(datatable)
end)

– Server script

-- Gets called when a player leaves the game
Players.PlayerRemoving:Connect(function(player)
	FinishedSaveGuiEvent.OnServerEvent:Connect(function(player, datatable)
		-- Save the datatable to the datastore, so the next time the player joins, it will load their data
		local success, errorMessage = pcall(function()
			DataStore:SetAsync(player.UserId, datatable)
			print(datatable)
		end)

		-- If there is an error saving the data, print an error message
		if not success then
			print(errorMessage)
		end
		print("finished") -- not getting called
	end)
	
	SaveGuiEvent:FireClient(player)
	print("fired")
end)

Do you know why?

I’m not using

Players.PlayerRemoving:Connect(function(player)

anymore so now I’m using:

-- Gets called when a player joins the game
Players.PlayerAdded:Connect(function(player)
	local success = pcall(function()				
		local data = DataStore:GetAsync(player.UserId)
		
		if data ~= nil then
			ChangeGuiEvent:FireClient(player, data)
		end
	end)
	
	FinishedSaveGuiEvent.OnServerEvent:Connect(function(player, datatable)
		-- Save the datatable to the datastore, so the next time the player joins, it will load their data
		local success, errorMessage = pcall(function()
			DataStore:SetAsync(player.UserId, datatable)
			print(datatable)
		end)

		-- If there is an error saving the data, print an error message
		if not success then
			print(errorMessage)
		end
	end)
	
	while wait(7) do
		SaveGuiEvent:FireClient(player)
	end
end)

but sometimes data doesn’t save can anyone help?