My in game currency suddenly stopped saving! HELP!

Hey guys, I’m working on a game right now, and it has in game currency. All of a sudden I noticed that this in game currency suddenly stopped saving. Here are the errors and the code.

local currencyName = "Coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("SaveMoney")
game.Players.PlayerAdded:Connect(function(player)
	
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	
	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder

    local ID = currencyName.."-"..player.UserId
    local savedData = nil
    
    pcall(function()
        savedData = DataStore:GetAsync(ID)
    end)
    
    if savedData ~= nil then
        currency.Value = savedData
        print("Data loaded")
    else
        --New player
        currency.Value = 10
        print("New player to the game")
    end


end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = currencyName.."-"..player.UserId 
	DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()

   --When game is ready to shut down
   
   for i, player in pairs(game.Players:GetPlayers()) do
	   if player then
		   player:Kick("This game is shutting down")
	   end
   end

   wait(5)

end)


And thats the error.
Please help, this is a horrible issue I have encountered!

Also how do I fix up that code block?

When you will create a topic, it will show how to make a code block.

As for your error, it means you are sending a lot of GetAsync or SetAsync.

So how do I stop sending lots of that?

I don’t see how you will send a lot of requests in that code. Do you have any other scripts that uses Datastore? Do you have an admin? (Basic Admin Essentials, Adonis Admin, etc)

1 Like

Well, I have a shop that saves what you’ve bought and also a daily reward GUI.

That may be the culprit. Based on how you said it, that’s not an efficient way to handle it, considering if there is a lot of players that presses the button at the same time. What I suggest is to do a auto save or save when a player leaves the game.

1 Like

I thought this one was already a save when the player leaves the game?

Your errors seems to be a result from your other scripts. The code you provided is alright, only other scripts that uses Datastore is the problem.

1 Like

Should I show my other scripts?

Instead of that, you can do what I suggested.

This Script is making my Datastore fail I am not the best coder, unfortunately.

I dont know if this will help but I wrote a script that may help. I really dont know though, as I dont see anything wrong with your code:

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("SaveMoney")

local tries = 3
local dataLoaded = false

local currencyName = "Coins"
local starterCoins = 10

local function set(plr)
	if dataLoaded then
		local key = currencyName .. "-" .. plr.UserId
		local data = {
			["Coins"] = starterCoins,
		}
		
		local count = 0
		local success, err
		
		repeat
			success, err = pcall(function()
				dataStore:SetAsync(key, data)
			end)
			
			count = count + 1
		until success or count >= tries
		
		if not success then
			warn("Failed to set data. Error code: " .. tostring(err))
			
			return
		end
	else
		warn("Data has not been loaded. Please wait for data to be loaded. Plr name" .. plr.Name)
		
		return
	end
end

local function get(plr)
	local key = currencyName .. "-" .. plr.UserId
	local data
	
	local count = 0
	local success, err
	
	repeat
		success, err = pcall(function()
			data = dataStore:GetAsync(key)
		end)
		
		count = count + 1
	until success or count >= tries
	
	if not success then
		warn("Failed to read data. Error code: " .. tostring(err))
		
		plr:Kick("Failed to load data, please rejoin.")
		
		return
	end
	
	if success then
		if data then
			dataLoaded = true
			
			return data
		else
			dataLoaded = true
			
			return {
				["Coins"] = starterCoins
			}
		end
	end
end

local function createLeaderstats(plr)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = plr
	
	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder
	
	local data = get(plr)
	
	currency.Value = data[currencyName]
end

players.PlayerAdded:Connect(createLeaderstats)
players.PlayerRemoving:Connect(set)

game:BindToClose(function()
   for i, player in pairs(game.Players:GetPlayers()) do
	   if player then
		   set(player)
	   end
   end
end)

Untested as well btw…

1 Like

Try changing the name of the DataStore, this happens to me sometimes even when there’s nothing wrong with my code and then I change the name of the DataStore and it suddenly starts working (in Studio)

Right now you have “SaveMoney” as the datastore so you could try changing the name just to test and see if it works. (ex. change it to “MoneySave”)

Also sometimes if it doesn’t work in Studio, test in the real game just to make sure it doesn’t work because sometimes for me it doesn’t work in studio and then I go in game and it works (gets me frustrated cause I used to spend so much time trying to debug :joy:)

1 Like

With yours it kicks me from the game and says : you were kicked from the game- data didn’t load.

I dont know, you could try changing the data store name. It is untested by me so there could be a bug.

1 Like

Wait, you do have datastores enabled right?

1 Like

Yes it’s been fixed. The problem was fixed.

1 Like

ah, I didn’t see that. lol well glad you fixed it