Saving Coin when player leave and rejoin

What do you want to achieve? Keep it simple and clear!
When a player join for the first time, he would be able to see and collect the coin. Once he collected it, the coin would dissapear and the next time when he joins again the game, the coin will remain dissapearing (He would not be able to collect it again).
What is the issue? Include screenshots / videos if possible!
With my script, the coin dissapears once I collect it for the first time but when I rejoin the game for a second time, the coin remains still visible to me and I can still collect it. I have a BoolValue in each Coin with original value set to False.

Below iis the script that I use in each coin.

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local settings = require(workspace.Imaginary.Configurations)
local SaveDataStore = DSS:GetDataStore(settings.datastoreName)
local part = script.Parent


part.Touched:Connect(function(hit)
	local Plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	local Award = part.Value
	local CheckData 
	local success, Saved = pcall(function()
		CheckData = SaveDataStore:GetAsync(Plr.UserId)
	end)
	
	if success and CheckData then
		if Award.Value == true then
			part:Destroy()
		elseif Award.Value == false then
			Award.Value = true
			local success, Saved = pcall(function()
				SaveDataStore:SetAsync(Plr.UserId, Award.Value)
			end)

			if success then
				print("Successfully saved the data of Player", Plr.Name)
			else
				warn("Error occured:", Saved)
			end
		end
	end
end)

It’s reappearing because you’re only executing the code for the datastore when they touch the part, so the server doesn’t know yet that they’ve collected it. I’d address this by separating the datastore to .PlayerAdded()/.PlayerRemoved() functions and change the architecture of your stores to tables containing the coins the player has picked up, so when they join, you immediately know what coins they have/haven’t touched yet and can clone or destroy them accordingly, depending how your games’ application needs.

1 Like