Data store not working properly

Hello! I am currently trying to make a Find The… game where you have to find memes that are hidden around the map. It’s almost finished, but I am trouble getting the leaderstats to work properly. Even though I have reset the DataStore and scanned the code multiple times, nothing seems to be working. Here is my code:
DATA SAVER SERVER SCRIPT:

--Variables
local DataStoreService = game:GetService("DataStoreService")
local collectedDataStore = DataStoreService:GetDataStore("MemesCollected2000")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MemeList = ReplicatedStorage:WaitForChild("Memes")

game.Players.PlayerAdded:Connect(function(Player)
	local MemesCollected = Instance.new("Folder", Player)
	MemesCollected.Name = "MemesCollected"

	--Load collected data
	local success, errormessage = pcall(function()
		local memes = collectedDataStore:GetAsync("User-"..Player.UserId)
		if memes then
			for i, v in pairs(memes) do
				local memeFound = MemeList:FindFirstChild(v)
				if memeFound then
					memeFound:Clone().Parent = MemesCollected
				end
			end
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	--Saving collected memes
	local success, errormessage = pcall(function()
		local collectedSave = {}
		for i, meme in pairs(Player.MemesCollected:GetChildren()) do
			if meme then
				table.insert(collectedSave, meme.Name)
			end
		end
		collectedDataStore:SetAsync("User-"..Player.UserId, collectedSave)
	end)
	if success then
		print("Your data has been saved!")
	else
		print("Your data hasn't been saved!  Please try again later.")
		warn(errormessage)
	end
end)


game:BindToClose(function(Player)
	--Saving collected memes
	local success, errormessage = pcall(function()
		local collectedSave = {}
		for i, meme in pairs(Player.MemesCollected:GetChildren()) do
			if meme then
				table.insert(collectedSave, meme.Name)
			end
		end
		collectedDataStore:SetAsync("User-"..Player.UserId, collectedSave)
	end)
end)

LEADERSTATS SERVER SCRIPT:

local DataStoreService = game:GetService("DataStoreService")
local amountCollected = DataStoreService:GetDataStore("AmountCollected")

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"
	
	local Collected = Instance.new("IntValue", leaderstats)
	Collected.Name = "Collected"
	Collected.Value = 0
	
	local PlayerUserId = "Player_"..Player.UserId
	
	--Loading amount collected
	local amountCollectedData
	local success, errormessage = pcall(function()
		amountCollectedData = amountCollected:GetAsync(PlayerUserId)
	end)
	
	if success then
		print("success")
		Collected.Value = amountCollectedData
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local PlayerUserId = "Player_"..Player.UserId
	local collectedValue = Player.leaderstats.Collected.Value
	
	local success, errormessage = pcall(function()
		amountCollected:SetAsync(PlayerUserId, collectedValue)
	end)
end)

game:BindToClose(function()
	for _, Player in pairs(game.Players:GetPlayers()) do
		local PlayerUserId = "Player_"..Player.UserId
		local collectedValue = Player.leaderstats.Collected.Value

		local success, errormessage = pcall(function()
			amountCollected:SetAsync(PlayerUserId, collectedValue)
		end)
	end
end)

If you think you know what went wrong, please let me know. Thank you and have a wonderful day! :slight_smile:

5 Likes

is your game published to roblox?

Yes it is. It’s been awhile since I updated it, though.

what about enabling studio access to API services?

2 Likes

It is turned on, so I think something must be wrong with the code.

1 Like

you dont get any errors in the output for this script?

No, nothing. Nothing is in the output or the script analysis tab. I’ve reset the DataStore but the number of Memes that i’ve collected has stayed the same no matter what.
image
And I can’t think of any other scripts that could be interfering with this.

how many memes do you have in total? and by this you mean that when you collect a meme, it still stays at 26?

im trying to narrow down the problem because i cant seem to see any errors

also i think you accidentally closed the post

I have 22 set up currently. They have managed with values in a folder within ReplicatedStorage.

what is each script saving? if both are saving the same thing then maybe that could be causing an issue

They are both saving the collected memes, and if that is the problem, how would I fix it?

if one is saving the leaderboard value and the other is saving the actual memes discovered, maybe you should combine this into one script. when you save the actual memes youve found its a table so maybe instead of saving this as a separate value, you can check the length of the table to see what the leaderboard’s value should be instead of needing to save it all the time. if this doesnt solve it, then it will make the script more efficient.

also youve discovered 26 but you said theres only 22 memes, do different memes have different values or do they all add 1 to the counter?

Can you take the code I provided an give me a sample if you don’t mind?

yeah i will that was too complicated :sob:

game.Players.PlayerAdded:Connect(function(Player)

	local success, errormessage = pcall(function()
		collecteddata = collectedDataStore:GetAsync("User-"..Player.UserId) -- if this returns a table
	end)

	Player.leaderstats.Collected.Value = #collecteddata -- this must give the length of collecteddata which is the amount of memes the player has collected
end)

this means that you dont need to store the value of leaderstats

1 Like

Which script should I keep? The leaderstats or the DataSaver?

1 Like

also, does every meme collected increase this value by 1 or no?

No, it does not. The value does not go up or down. It stays at 26.

did this value change previously or has it always just been 26?

the data saver script, i was thinking since youre loading data here, then you can do this as well to prevent you needing to load data into leaderstats with another script. if you think changing this will cause issues in future then you can leave it alone

2 Likes

It just stopped working one day, and has not gone above or below 26.

3 Likes