How to see if someone has data

So I am trying to see if someone has data if they don’t they get the starting money of 100

but its not working

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
	
	print('ATM Loaded')
	local data
	if DataStore:GetAsync(player.UserId.."-Cash") ~= nil then
		Cash.Value = Configs.StartingMoney 
	else
		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
	


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)

It’s because you’re checking whether the player had data, and if they do, then you set their money to the starting money. For players that don’t have data, nothing happens. This would probably work better:

     local success, data = pcall(function()
			return DataStore:GetAsync(player.UserId.."-Cash", Cash.Value)
		end)
     if not success then 
         warn(data)
         return
     end
    if data then
        Cash.Value = data
    else
        Cash.Value = Configs.StartingMoney
	end
1 Like
local DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("DataStore")
local Players = game:GetService("Players")
local Replicated = game:GetService("ReplicatedStorage")
local Configs = Replicated:WaitForChild("ConfigsAtm")
local Settings = Configs:WaitForChild("Settings")
local Module = require(Settings)

local ProtectedCall = pcall

Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = Module.CurrencyName
	Cash.Parent = Leaderstats

	local Success, Result = ProtectedCall(function()
		return DataStore:GetAsync(Player.UserId.."-Cash")
	end)
	
	if Success then
		Cash.Value = Result or Module.StartingMoney
	else
		warn(Result)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local Success, Result = ProtectedCall(function()
		return DataStore:SetAsync(Player.UserId.."-Cash", Player.leaderstats[Module.CurrencyName.Value])
	end)
	
	if Success then
		print(Result)
	else
		warn(Result)
	end
end)

game:BindToClose(function()
	for _, Player in ipairs(Players:GetPlayers()) do
		local Success, Result = ProtectedCall(function()
			return DataStore:SetAsync(Player.UserId.."-Cash", Player.leaderstats[Module.CurrencyName.Value])
		end)

		if Success then
			print(Result)
		else
			warn(Result)
		end
	end
end)