The datasave script is not working

I’ve been stuck the past 2 days, trying to make datasave script but it does not work somehow…

Here is the code:

local DataStoreService = game:GetService("DataStoreService")
local MarketplaceService = game:GetService("MarketplaceService")
local myDataStore = DataStoreService:GetDataStore("PlayerLocker")
local RadioRemote = game:GetService("ReplicatedStorage"):FindFirstChild("Check_ItemsEquipped"):FindFirstChild("RadioBoolValue")
local RADIO_PASS_ID = 663864800

game.Players.PlayerAdded:Connect(function(Player)
	local userId = Player.UserId

	local FolderOwned = Instance.new("Folder")
	FolderOwned.Name = "ItemsOwned"
	FolderOwned.Parent = Player

	local RadioOwned = Instance.new("BoolValue")
	RadioOwned.Name = "RadioOwned"
	RadioOwned.Parent = FolderOwned

	local FolderEquipped = Instance.new("Folder")
	FolderEquipped.Name = "ItemsEquipped"
	FolderEquipped.Parent = Player

	local RadioEquipped = Instance.new("BoolValue")
	RadioEquipped.Name = "RadioEquipped"
	RadioEquipped.Parent = FolderEquipped
	
	RadioOwned.Value = MarketplaceService:UserOwnsGamePassAsync(userId, RADIO_PASS_ID)
	
	local data
	local success, errorMessage = pcall(function()
		data = myDataStore:GetAsync(userId.."_RadioEquipped")
	end)

	if success then
		RadioEquipped.Value = data
--		RadioRemote:FireClient(Player ,data)
		print("Successfully loaded the data for "..Player.Name.." | UserID: "..userId)
	else
		print("There was an error loading the data for "..Player.Name.." | UserID: "..userId)
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	print("PLAYER LEAVING!!!")
	
	local userId = Player.UserId
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(userId.."_RadioEquipped", Player.ItemsEquipped.RadioEquipped.Value)
	end)
	
	if success then
		print("Data successfully saved! for "..Player.Name.." | UserID: "..userId)
	else
		print("There was an error saving the data for "..Player.Name.." | UserID: "..userId)	
		warn(errormessage)
	end
end)
2 Likes

I’d suggest using
game:BindToClose(function() wait(5) end)
You’d keep the code as is I believe, but just put this at the very end. Sometimes when attempting to save the players data, the server closes before it can fully go through with it all.

2 Likes

It did work and said it saved it but when I played it again it didn’t put the previous values in there.

Is there any Errors in an Output or Developer Console?

no. There isn’t any error in the dev console.

Do you want to replicate the value to the client or not?