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)
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)
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.
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)
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 )