Hello there! I wanted a currency system that saves with a dataStore. I followed a tutorial online by AlvinBlox to get my script. I have checked if I have toggled the API checkbox - thing. Which I have toggled it.
The issue is that the datastore isn’t loading any new values for my Coins. For example, if I pick up 5 coins in my game, close it, then re-open, it would set it to the default starter amount.
I have looked for solutions to this, but I haven’t found much. Every time I tried to fix something, it basically just got worse.
I want to know if I’m doing anything wrong with my script. Any help or feedback is greatly appreciated!
local currencyName = "Coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("CoinDataStore")
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(player)
savedData = DataStore:GetAsync(ID)
end)
if savedData ~= nil then
currency.Value = savedData
print("Data loaded")
else
currency.Value = 2500
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()
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("This game is shutting down! Please rejoin.")
end
end
wait(5)
end)
You should always pcall any communications with roblox APIs (you forgot to in the player remove function)
You can remove the bind to close part, delaying the game shutdown by 5 seconds does not help. There used to be a problem with datastore where you would have to implement bind to close logic for it to work on the last player. To my knowledge, this issue no longer exists.
Do you have any errors?
The killer of all datastore testing. I have done this countless times. When you add the currency to the intvalue, make sure you are doing it from the SERVER side, not the CLIENT side. If you add it from the client side, the server will not be able to read it, thus it will not save.
Theres no need to have player as an argument in the pcall function. It will never be assigned by pcall.
I have no errors, that’s why this is confusing me…
This is my updated script, taking your advice. It didn’t work, so the main problem might be number 4.
I’m not exactly sure how to implement number 4 into the script, however. Any help would be greatly appreciated!
local currencyName = "Coins"
local DataStore = game:GetService("DataStoreService"):GetDataStore("CoinDataStore")
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
currency.Value = 2500
print("New player to the game")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
pcall(function()
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)
end)
So I had this issue too. I think it has to do with not getting enough time to save your data, for example, In game the Roblox server stays open for 30 seconds (plenty of time to save data), but in Studio the server closes almost immediately (not nearly enough time to save data). You could make a Autosave system, or in game it should work.
Hi Guys, I was having various issues with coins + data store + leader stats etc. Took a while but I found a solution that works for me and may be helpful to you as well. Its a coin system with different valued coins up to “100”. The model has $5850 worth of coins for you to spread around your map. It comes with detailed instructions and all coins collected add up properly and save for your next visit! (9) CoinCollectingAndSavingSystem - Roblox