:GetAsync() is always setting value to 0

I’m attempting to create a developer product that will increase the value of an IntValue inside a player’s leaderstats folder upon purchasing the product and the updated value will be backed up by a datastore. In this instance; the IntValue is named “Kills”.

Problem is that when the :GetAsync() function is called, the value is set to zero instead of the new value that was updated for the player. (Video showcasing problem below)

Code:
Script that increases the player’s kills value after the transaction:

local MarketPlaceService = game:GetService("MarketplaceService")

MarketPlaceService.ProcessReceipt = function(receiptInfo)
	print(receiptInfo)
	
	local playerId = receiptInfo.PlayerId
	local productId = receiptInfo.ProductId
	
	if productId == 2684175514 then -- My developer product ID
		local player = game.Players:GetPlayerByUserId(playerId)
		
		player.leaderstats.Kills.Value += 50
	end
end

Leaderstats script:

local DataStoreService = game:GetService("DataStoreService")
local PlayerStats = DataStoreService:GetDataStore("PlayerStats")
local PlayerKills = DataStoreService:GetDataStore("PlayerStats", "Kills")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"
	kills.Parent = leaderstats
	
	while task.wait(5) do
		local success, currentKills = pcall(function()
			return PlayerKills:GetAsync(plr.UserId)
		end)
		if success then
			kills.Value = currentKills
		end
	end
end)

Is this a Roblox problem or is it just me? (Most likely)
Any help would be appreciated, cheers!

1 Like

Remove this code:

while task.wait(5) do
    local success, currentKills = pcall(function()
        return PlayerKills:GetAsync(plr.UserId)
    end)
    if success then
        kills.Value = currentKills
    end
end

All it’s doing is repeatedly loading in the current value in the data-store. There’s zero need for you to do this, and since I see no other code updating the value in the data-store, it will continue to overwrite your “kills” with the previously stored value

1 Like

Seems like I’ve just figured it out!

So I’ve rewritten the specified code to not contain the while loop and I’ve implemented DataStoreService into the leaderstats script. Instead of incrementing the value using the “+=” operators, I’ve used IncrementAsync() and it appears to work!

if productId == 2684175514 then
		local player = game.Players:GetPlayerByUserId(playerId)
		
		local success, updatedKills = pcall(function()
			return PlayerKills:IncrementAsync(player.UserId, 50)
		end)
		if success then
			player.leaderstats.Kills.Value = updatedKills
		end
	end

Thanks a bundle!

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