I am using 2 scripts 1 to create the IntValue and 1 to save it but its not saving in roblox studio and outside roblox studio (game itself)
Data script
game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder", player) leaderstats.Name = "leaderstats" local money = Instance.new("IntValue", leaderstats) money.Name = "Money" money.Value = 0 end)
Save Script:
local ds = game:GetService("DataStoreService"):GetDataStore("SaveData") game.Players.PlayerAdded:Connect(function(player) wait() local playerkey = "id_"..player.UserId local saves = player.leaderstats.Money local save = ds:GetAsync(playerkey) if save then saves.Value = save[1] else local numbersave = {saves.Value} ds:GetAsync(playerkey, numbersave) end end) game.Players.PlayerRemoving:Connect(function(player) ds:GetAsync("id_"..player.UserId, {player.leaderstats.Money.Value}) end)
The script i use to increase the money value of the player:
local payCheckTime = 25 while true do wait(payCheckTime) Plr.leaderstats.Money.Value = Plr.leaderstats.Money.Value + 25 end
Before getting into the actual problem, why do you have two different scripts for creating leaderstats and loading and saving them? These all could be in one script to make things easier. Also, you shouldn't really try solving issues by adding an unnecessary wait()
at the beginning -- WaitForChild exists.
Going into the actual problem now, there are a few reasons as to why your data doesn't save:
So, how can the two issues below be solved? First, you would need to implement a BindToClose callback. Whenever the server is scheduled to shut down, BindToClose
will make the server wait a maximum of 30 seconds before the server can shut down and execute all callbacks delegated to the function. This is where you can save the data for all players and the server will shut down when all of the data has been saved (or when 30 seconds has been elapsed)
As for the last issue, this can be solved by implementing a retry system that is limited to a specific amount of retries. You will have to use pcall
so you save the data in protected mode where errors that occur doesn't terminate further execution of the code but indicates whether or not it ran successfully
Also, it's easier to use a dictionary to save and retrieve the data. All together, here's what the code should be:
local ds = game:GetService("DataStoreService"):GetDataStore("SaveData") local Players = game:GetService("Players") function SaveData(player) local MAX_TRIES = 5 local tries = 0 local saveData = { Money = player.leaderstats.Money.Value; } repeat local success = pcall(function() ds:SetAsync("id_"..player.UserId, saveData) end) wait(6) tries += 1 until success or tries >= 5 end function GetData(player) local MAX_TRIES = 5 local tries = 0 local result repeat local success = pcall(function() result = ds:GetAsync("id_"..player.UserId) end) wait(7) tries += 1 until success or tries >= 5 return result end game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local money = Instance.new("IntValue") money.Name = "Money" money.Value = 0 local data = GetData(player) if data then money.Value = data.Money end end) game.Players.PlayerRemoving:Connect(SaveData) game:BindToClose(function() local player_list = Players:GetPlayers() local player_count = #player_list local saved_players = {} for _, player in ipairs(player_list) do task.spawn(function() SaveData(player) table.insert(saved_players, player) end) end while (#saved_players < player_count) do task.wait() end end)
Have you enabled Studio Access to API Services? To do so, go to the Roblox website's develop section and configure your game's experience. Click on "Enable Studio Access to API Services", and try again. EnzoTDZ_YT's code is probably the best one in this scenario though.
You are using GetAsync() to save the value. You should be using SetAsync()
game.Players.PlayerRemoving:Connect(function(player) ds:SetAsync("id_"..player.UserId, {player.leaderstats.Money.Value}) -- Change get to set to save the value. end)