What is wrong with this Datastore Visibility script?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    So, I have a script that if the player buys a product it will set a GUI’s visibility to true.

  2. What is the issue? Include screenshots / videos if possible!
    It works, It sets the Visibility to true, the prints are printing. But, it isn’t Saving its Visibility property whenever a player rejoins.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked everywhere. It is a working script and the prints are printing. But, it isn’t saving.

This is my ServerScript located inside of ServerScriptService

local MPS = game:GetService("MarketplaceService")
local players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local DonationStore = DSS:GetDataStore("DonationStore")

local ProductID50 = 1750469452

local function proccessReceipt(receiptInfo)
	local playerID = game:GetService("Players"):GetPlayerByUserId(receiptInfo.PlayerId)

	if not playerID then
		print("No player")

		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if receiptInfo.ProductId == ProductID50 then
		if playerID then
			playerID.PlayerGui.BadgeSelect.main.DonationBadge.Visible = true
			task.wait(1)
			local succ,err = pcall(function()
				DonationStore:SetAsync(playerID.UserId, playerID.PlayerGui.BadgeSelect.main.DonationBadge.Visible)
			end)
			if succ then
				print("successfully saved Data")
			end
			if err then
				print(err)
			end
		end
	end
	


	return Enum.ProductPurchaseDecision.PurchaseGranted
end

MPS.ProcessReceipt = proccessReceipt

		
local function HandlePlayer(playerID)
	local succ,err = pcall(function()
	    DonationStore:GetAsync(playerID.UserId)
	end)
	if succ then
		print("successfully loaded donation")
	end
	if err then
		print(err)
	end
end

for _, Player in pairs(game.Players:GetPlayers()) do
	HandlePlayer(Player)
end

game.Players.PlayerAdded:Connect(HandlePlayer)	

so here is the issue and solution:

  • This function processes purchase receipts.

  • It first attempts to retrieve the player associated with the receipt’s PlayerId.

  • If no player is found, it prints “No player” and returns NotProcessedYet.

  • If the product ID in the receipt matches ProductID50, it proceeds to set the visibility of a badge in the player’s GUI.
    he ProcessReceipt function is assigned to MPS.ProcessReceipt . This likely hooks it into the marketplace service for processing receipts.
    This function retrieves data from the DonationStore for a given player.

    • It uses GetAsync to fetch the data associated with the player’s UserId.
    • If successful, it prints “successfully loaded donation.”
    • If there’s an error, it prints the error message.
  • function is assigned to MPS.ProcessReceipt. This likely hooks it into the marketplace service for processing receipts.

  • The script iterates through all players currently in the game.

  • For each player, it calls the HandlePlayer function to load donation data.

  • The script connects the HandlePlayer function to the PlayerAdded event.

  • This ensures that when a new player joins the game, their donation data is loaded.

Now, let’s address the issues in your script:

  • On line 31, you have a typo: “cash” instead of “Cash.”
  • Change it to: CashStore:SetAsync(Player.UserId.."-cash", Player.leaderstats.Cash.Value).
  • You mentioned that you’re testing saving data on PlayerRemoving.
  • Keep in mind that in Studio, the server closes too quickly for you to observe the data being saved.
  • To test this properly, run the game in an actual Roblox environment.
  • You’ve included a BindToClose function, but it doesn’t do anything yet.
  • Consider saving data for players still in the game during server shutdown.

Thanks for the reply. But, I am quite confused.

  • You said, on line 30 I have a typo: “cash” instead of “Cash”. Nowhere in my script is there something called “Cash”

  • My script has DonationStore, not CashStore and it does not use leaderstats. I am trying to save a Gui’s Visibility Value.

  • It seems that I have worded that poorly, and it is my fault. SetAsync fires when the player has purchased the product, not when they leave the game. And then it fires GetAsync when the player joins the game. It is both printing as successful, but not actually saving the data when I rejoin.

  • I also do not have a BindToclose function.

chatgpt reply dont listen to this guy please

3 Likes

here solution replace your HandlePlayer function with this

local function HandlePlayer(playerID)
	local succ,data = pcall(function()
	    return DonationStore:GetAsync(playerID.UserId)
	end)
	if succ and data then
        playerID:WaitForChild("PlayerGui"):WaitForChild("BadgeSelect"):WaitForChild("main"):WaitForChild("DonationBadge").Visible = true
		print("successfully loaded donation")
        return
	end
	if data then
		warn("errored",data)
	end
end

true, roblox devforum should ban chatgpt just like stackoverflow. (but thats for another topic)

oh, brotherrrrrrrr. THIS GUY STINKS!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.