Help with leaderstats Data Store

Hello, I am not gettings any errors but this data store script (I copy off Roblox DataStore Tutorial - Data Stores & Saving Data - Scripting Tutorial - YouTube) It does not load data.

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("DataStore")--Change the name of this if you want to wipe the player's data

local Configs = require(game.ReplicatedStorage.ConfigsAtm.Settings)

local player = game.Players.LocalPlayer

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local Cash = Instance.new("IntValue")
	Cash.Name = Configs.CurrencyName
	Cash.Parent = leaderstats
	Cash.Value = Configs.StartingMoney 
	print('ATM Loaded')
	local data
	local succese, errorMessage = pcall(function()
		data = DataStore:GetAsynce(player.UserId.."-Cash", Cash.Value)
	end)
	if succese then
		Cash.Value = data
		print(data)
	else
		print("Error when saving data")
		warn(errorMessage)
	end
	
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
 local succese, errorMessage = pcall(function()
	DataStore:SetAsynce(player.UserId.."-Cash", player.leaderstats[Configs.CurrencyName].Value)
	end)
	if succese then
		print('Saved Data Looping')
	else
		print("Error when saving data")
		warn(errorMessage)
	end
	
	
end)

Have you enabled API Services in game settings?

1 Like

Try testing in the actual game. Studio closes the game before PlayerRemoving runs. If you want to fix it within Studio, then use game:BindToClose().

local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("DataStore")--Change the name of this if you want to wipe the player's data

local Configs = require(game.ReplicatedStorage.ConfigsAtm.Settings)

local player = game.Players.LocalPlayer

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local Cash = Instance.new("IntValue")
	Cash.Name = Configs.CurrencyName
	Cash.Parent = leaderstats
	Cash.Value = Configs.StartingMoney 
	print('ATM Loaded')
	local data
	local succese, errorMessage = pcall(function()
		data = DataStore:GetAsync(player.UserId.."-Cash", Cash.Value)
	end)
	if succese then
		Cash.Value = data
		print(data)
	else
		print("Error when saving data")
		warn(errorMessage)
	end
	
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
 	local succese, errorMessage = pcall(function()
		DataStore:SetAsync(player.UserId.."-Cash", player.leaderstats[Configs.CurrencyName].Value)
	end)
	if succese then
		print('Saved Data Looping')
	else
		print("Error when saving data")
		warn(errorMessage)
	end
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		 local succese, errorMessage = pcall(function()
			DataStore:SetAsync(player.UserId.."-Cash", player.leaderstats[Configs.CurrencyName].Value)
		end)
		if succese then
			print('Saved Data Looping')
		else
			print("Error when saving data")
			warn(errorMessage)
		end
	end
end)

Also, you completely misspelled SetAsync and GetAsync (you spelled them as GetAsynce and SetAsynce).

2 Likes

Are you missing the necessary configuration script referenced at the top of the script?

Alright, I did not know studio did it.

game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		local succese, errorMessage = pcall(function()
			DataStore:SetAsynce(player.UserId.."-Cash", player.leaderstats[Configs.CurrencyName].Value)
		end)
		if succese then
			print('Saved Data Looping')
		else
			print("Error when saving data")
			warn(errorMessage)
		end
	end
end)

As the above user suggested, adding a BindToClose() function to your script will allow for the saving to work while in studio (as PlayerRemoving sometimes doesn’t fire when in studio).

1 Like