:GetAsync() returns nil whenever Data is created

Hello, I’m developing game that stores datas using DataStore .
I tried to rewrite datastore code and code looks like this right now:

local dataService = game:GetService("DataStoreService")
local coinData = dataService:GetDataStore("PlayerCoinData")
local trailData = dataService:GetDataStore("PlayerTrailData")

function checkData(plr)
	local suc,err = pcall(function()
		coinData:GetAsync(tostring(plr.UserId))
	end)
	
	local suc2,err2 = pcall(function()
		trailData:GetAsync(tostring(plr.UserId))
	end)
	return suc,suc2
end

function updateData(plr)
	local function getCoins()
		return plr.GameConfigs.ObbyCoins.Value
	end
	local suc,err = pcall(function()
		coinData:UpdateAsync(tostring(plr.UserId),getCoins)
	end)
	if suc then
		print("Done!")
		print(plr.GameConfigs.ObbyCoins.Value)
	else
		error(err)
	end
end

function loadData(plr)
	local suc,dat = pcall(function()
		coinData:GetAsync(tostring(plr.UserId))
	end)
	if suc then
		print(suc,dat)
		plr.GameConfigs.ObbyCoins.Value = dat
	else
		error(dat)
	end
	
end

function newData(plr)
	local suc,err = pcall(function()
		coinData:SetAsync(tostring(plr.UserId),0)
	end)
	
	local suc2,err2 = pcall(function()
		trailData:SetAsync(tostring(plr.UserId),{})
	end)
	if suc and suc2 then
		print("Done!")
	else
		error(err .."  and  ".. err2)
	end
	
end

game.Players.PlayerAdded:Connect(function(player)
	local a,b = checkData(player)
	print(a,b)
	print( not a or not b )
	if not a or not b then
		newData(player)
	else
		loadData(player)
	end
end)

local autosave = coroutine.wrap(function(interval)
	while wait(interval) do
		for i,v in ipairs(game.Players:GetPlayers())do
			updateData(v)
		end		
	end
end)

autosave(30)


game.Players.PlayerRemoving:Connect(function(player)
	updateData(player)
end)

But when I run this script, It prints these:
Capture

“True nil” output comes from loadData() function, “true true” output comes from checkData() function.

And I attempted to save and load data before via script, and it worked well.

That’s a bug? Or maybe my code is faulty?

3 Likes

local suc,dat = pcall(function()
coinData:GetAsync(tostring(plr.UserId))
end)
“dat” does not return the data itself, instead it returns if there was errors. Instead make a variable and call it CoinData and set CoinData = coinData:GetAsync(tostring(plr.UserId)). And that variable will be what your data is.

Here is an example.
local dataService = game:GetService(“DataStoreService”)
local coinData = dataService:GetDataStore(“PlayerCoinData”)
local trailData = dataService:GetDataStore(“PlayerTrailData”)

function checkData(plr)
	local suc,err = pcall(function()
		coinData:GetAsync(tostring(plr.UserId))
	end)
	
	local suc2,err2 = pcall(function()
		trailData:GetAsync(tostring(plr.UserId))
	end)
	return suc,suc2
end

function updateData(plr)
	local function getCoins()
		return plr.GameConfigs.ObbyCoins.Value
	end
	local suc,err = pcall(function()
		coinData:UpdateAsync(tostring(plr.UserId),getCoins)
	end)
	if suc then
		print("Done!")
		print(plr.GameConfigs.ObbyCoins.Value)
	else
		error(err)
	end
end

function loadData(plr)
	local coins
	local suc,dat = pcall(function()
		coins = coinData:GetAsync(tostring(plr.UserId))
	end)
	if suc then
		print(suc,dat)
		plr.GameConfigs.ObbyCoins.Value = coins
	else
		error(dat)
	end
	
end

function newData(plr)
	local suc,err = pcall(function()
		coinData:SetAsync(tostring(plr.UserId),0)
	end)
	
	local suc2,err2 = pcall(function()
		trailData:SetAsync(tostring(plr.UserId),{})
	end)
	if suc and suc2 then
		print("Done!")
	else
		error(err .."  and  ".. err2)
	end
	
end

game.Players.PlayerAdded:Connect(function(player)
	local a,b = checkData(player)
	print(a,b)
	print( not a or not b )
	if not a or not b then
		newData(player)
	else
		loadData(player)
	end
end)

local autosave = coroutine.wrap(function(interval)
	while wait(interval) do
		for i,v in ipairs(game.Players:GetPlayers())do
			updateData(v)
		end		
	end
end)

autosave(30)


game.Players.PlayerRemoving:Connect(function(player)
	updateData(player)
end)
3 Likes

Oh, I thought dat will return coin data. That’s why my coins doesn’t loading.
Thank you!