"attempt to call a string value" while using DS:SetAsync()

So while using SetAsync with datastore service I ran across an issue where it wouldnt work and throw an error “attempt to call a string value”. It didn’t directly give me the error message but here’s my code:

module.SaveData = function(plr)
	-- Save player data --
	print(plr.Name.." is having their data saved")
	
	-- Get player's current stats --
	local leaderstats = plr:WaitForChild("leaderstats")
	local coins = leaderstats:WaitForChild("Coins").Value
	local regen = plr:WaitForChild("RegenUpgrade").Value
	local reward = plr:WaitForChild("RewardUpgrade").Value
	
	-- Store in table and save it to datastore --
	while true do
		local s, e = pcall(coinsDS:SetAsync(plr.UserId, coins)) 
		local s2, e2 = pcall(regenUpgDS:SetAsync(plr.UserId, regen)) 
		local s3, e3 = pcall(rewardUpgDS:SetAsync(plr.UserId, reward))
		if e or e2 or e3 then 
			task.wait(retryTime) 
			print("Error while saving "..plr.Name.."'s data, retrying.")
			print(e)
			print(e2)
			print(e3)
			print(s)
			print(s2)
			print(s3)
			continue
		end
		
		break
	end
end

The datastores whos’ asyncs are being set are equal to DSS:GetDataStore(“Name”)

When printing all the successes and the errors, all the successes equaled false and the errors were what I stated above.

It’s because you aren’t passing the setasync fn to pcall but the result of setasync. Pcall will then attempt to call this result (apparently a string) and throw.

You use pcall like this:

pcall(fn, ... any args to pass to fn)

Replace all of your setasync calls with:

--this is how you call pcall.
--note: no colon, use dot notation.
pcall(coinsDS.SetAsync, plr.UserId, coins)

Hope this helps!

1 Like

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