Failing to Datastore

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local Player_Coins = DataStoreService:GetDataStore("Player_Coins")
local Player_Wins = DataStoreService:GetDataStore("Player_Wins")
local Player_Kills = DataStoreService:GetDataStore("Player_Kills")

local function Updated_Player_Coins(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	
	if not leaderstats then
		error("Failed to find player leaderstats for ".. player.UserId .. " or " .. player.Name)
	end
	
	local Coins = leaderstats:FindFirstChild("Coins")
	
	return Coins.Value
end

local function Updated_Player_Wins(player)
	local leaderstats = player:FindFirstChild("leaderstats")

	if not leaderstats then
		error("Failed to find player leaderstats for ".. player.UserId .. " or " .. player.Name)
	end

	local Wins = leaderstats:FindFirstChild("Wins")

	return Wins.Value
end

local function Updated_Player_Kills(player)
	local leaderstats = player:FindFirstChild("leaderstats")

	if not leaderstats then
		error("Failed to find player leaderstats for ".. player.UserId .. " or " .. player.Name)
	end

	local Kills = leaderstats:FindFirstChild("Kills")

	return Kills.Value
end

local function Save_Player_Data(player, player_Coins, player_Wins, player_Kills)
	local success, updated_Coins = pcall(function()
		return Player_Coins:UpdateAsync(player.UserId, Updated_Player_Coins(player))
	end)
	
	if not success then
		error("Failed to update Player Coins for ".. player.UserId .. " or " .. player.Name)
	end
	
	local success, updated_Wins = pcall(function()
		return Player_Wins:UpdateAsync(player.UserId, Updated_Player_Wins(player))
	end)
	
	if not success then
		error("Failed to update Player Wins for ".. player.UserId .. " or " .. player.Name)
	end
	
	local success, updated_Kills = pcall(function()
		return Player_Kills:UpdateAsync(player.UserId, Updated_Player_Kills(player))
	end)
	
	if not success then
		error("Failed to update Player Kills for ".. player.UserId .. " or " .. player.Name)
	end
end

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Value = 0
	Coins.Parent = leaderstats
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Value = 0
	Wins.Parent = leaderstats
	
	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills"
	Kills.Value = 0
	Kills.Parent = leaderstats
	
	local success, current_Coins = pcall(function()
		return Player_Coins:GetAsync(player.UserId)
	end)
	
	if not success then
		Coins.Value = 0
		error("Failed to retrieve Player Coins for ".. player.UserId .. " or " .. player.Name)
	else
		Coins.Value = current_Coins
	end
	
	local success, current_Wins = pcall(function()
		return Player_Coins:GetAsync(player.UserId)
	end)
	
	if not success then
		Wins.Value = 0
		error("Failed to retrieve Player Wins for ".. player.UserId .. " or " .. player.Name)
	else
		Wins.Value = current_Wins
	end
	
	local success, current_Kills = pcall(function()
		return Player_Coins:GetAsync(player.UserId)
	end)

	if not success then
		Kills.Value = 0
		error("Failed to retrieve Player Kills for ".. player.UserId .. " or " .. player.Name)
	else
		Kills.Value = current_Kills
	end
end)

Players.PlayerRemoving:Connect(Save_Player_Data)

game:BindToClose(function(Close_Reason)
	for i, player in Players:GetChildren() do
		Save_Player_Data(player)
	end
end)


Screenshot 2025-07-22 134709

Hello! I’m new to datastore and I’m trying to make a datastore system that saves the player’s data when they enter / leave the game but I’m getting these warns / errors and I don’t know what they mean and how to fix them.

1 Like

Instead of using :UpdateAsync you should use :SetAsync.

You’re trying to use the 2nd variable pcall() gives you to get the value from the DataStore. This is wrong! The 2nd value that pcall returns is an error message if there is one. If there isn’t, it just returns nil.

Considering this, you should store your value in another variable, outside the pcall.

For example, instead of doing this:

	local success, updated_Wins = pcall(function()
		return Player_Wins:SetAsync(player.UserId, Updated_Player_Wins(player))
	end)

you should do this:

local updated_Wins 
local success, errorMessage = pcall(function()
	updated_Wins = Player_Wins:SetAsync(player.UserId, Updated_Player_Wins(player))
end)

This way your updated_Wins variable will contain the proper data. You will have to do this for every pcall in your code.

2 Likes

Dang, I thought the 2nd parameter returns the value of what I’m trying to return

Also why should I use :SetAsync over UpdateAsync? I’ve been told that UpdateASync is better

For something as simple as you’re trying to do, :SetAsync will suffice. Unless you’re doing datastore lookups on the same keys/values for multiple servers at the same time it’s not needed and will eat up your DataStore budget.

Another important thing to note is that both :SetAsync and :UpdateAsync don’t return the value they save. Don’t try to retrieve it because it won’t work.

Ah okay, I’ll change it to :SetAsync and do you think my function for returning every stats value is efficient or could be better? :thinking:

The function is fine. You’re just returning the value of what I assume is a NumberValue.

You could define leaderstats at the top of the script instead of trying to find it every time for every function.

Oh alright, thank you for your help! :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.