Problem with DataStore

A working DataStore that shows money in the .Text property of a TextLabel.

DataStore isn’t functioning. I have tried printing it rather than doing it directly to the textbox and I have not been successful.

(Both scripts are located in ServerScriptService)
Here is the PlayerAdded script:


game.Players.PlayerAdded:Connect(function(player)
	local Cash = player:WaitForChild("PlayerGui").MainMenu.Inventory.CashAmount.Text
    local Bank = player:WaitForChild("PlayerGui").MainMenu.Inventory.BankAmount.Text
	
	local key = "player-"..player.UserId
	
	local savedAmount = DataStore:GetAsync(key)
	
	if savedAmount then
		Cash = savedAmount[1]
		Bank = savedAmount[2]
	else
		local toSave = {Cash, Bank}
		DataStore:SetAsync(key, toSave)
	end
end)

Here is the PlayerRemoving script:


game.Players.PlayerRemoving:Connect(function(player)
	local Cash = player:WaitForChild("PlayerGui").MainMenu.Inventory.CashAmount.Text
    local Bank = player:WaitForChild("PlayerGui").MainMenu.Inventory.BankAmount.Text
	
	local key = "player-"..player.UserId
	
	local toSave  = {Cash, Bank}
	DataStore:SetAsync(key, toSave)
end)```

You can’t get GuiObjects from a player on a server script since it is only visible on the client, you’ll want to use remotes for checking player data (or better yet store them on the server). You can find more info on updating data here and usage on remotes here.

2 Likes

Ah thank you very much :grinning:, I didn’t think it was necessary to use Remote Events for this.