Datastore returns table as boolean value. Why?

Hello,

I want to create a system that tracks the amount of in-game currency (gems) spent on items. This system should allow people to see the average selling price of a particular item. It needs to work on every server, and whenever an event occurs, the information should be stored in a datastore. The stored value is then retrieved and distributed to all players on each server, including those outside my own server. However, I’m encountering an issue where an error occurs when the event is triggered, displaying
"ServerScriptService.boardscript:164: attempt to index boolean with number "

I hope to get assistance with this problem. I haven’t fully mastered the datastore yet, so I’m unable to come up with a solution on my own. I know how to get an individual value for a player, but I’m unsure how to make it the same for everyone in a table. If you have any questions or need clarification on what I’m doing, feel free to ask.

Here is the code:

local boardimage = ""
local images = {
	"rbxassetid://14085555656",	--rareimage
	"rbxassetid://14716948826",	--epicimage
	"rbxassetid://14717101543",	--legendaryimage
	"rbxassetid://14971358701",	--exclusiveimage
	"rbxassetid://15545891280",	--shieldimage
	"rbxassetid://15598046397",	--vipassimage
	"rbxassetid://14763896866", 	--ttTicketimage
	"rbxassetid://15586993560",
	"rbxassetid://15587000207",
	"rbxassetid://15587004973",
	"rbxassetid://15587009892",
	"rbxassetid://15587013605",
	"rbxassetid://15587017990",
	"rbxassetid://15587023725",
	"rbxassetid://15587028019",
	"rbxassetid://15587031382",
	"rbxassetid://15587034799",
	"rbxassetid://15587038053",
}

local sellvalueslist = {
	["item1"] = 0,
	["item2"] = 0,
	["item3"] = 0,
	["item4"] = 0,
	["item5"] = 0,
	["item6"] = 0,
	["item7"] = 0,
	["item8"] = 0,
	["item9"] = 0,
	["item10"] = 0,
	["item11"] = 0,
	["item12"] = 0,
}
game.ReplicatedStorage.changevalue.event.OnServerEvent:Connect(function(player, plr, seller, gemprice, rarity, amount)
	datastore:SetAsync("sellvalueslist", sellvalueslist)
	local oldvalue, errormsg = pcall(function()
		return datastore:GetAsync("sellvalueslist")
	end)
	if oldvalue then
		print("OLDVALUE")
		for i = 1, #images do
			if type(oldvalue) == "table" then
				print("IT IS TABLE")
			end
			print("Checking image", i, boardimage, images[i])
			if boardimage == images[i] then
				print("Found matching image")
				local gemprice3 = gemprice + oldvalue[i]
				local gemprice2 = gemprice / 2
				print("Gemprice values:", gemprice, gemprice2, gemprice3)
				sellvalueslist[i] = gemprice2
				print("Updated sellvalueslist", sellvalueslist[i])
				break
			end
		end
		datastore:SetAsync("sellvalueslist", sellvalueslist)
	end
	if errormsg then
		warn(errormsg)
		sellvalueslist = {
			["item1"] = 1,
			["item2"] = 1,
			["item3"] = 1,
			["item4"] = 1,
			["item5"] = 1,
			["item6"] = 1,
			["item7"] = 1,
			["item8"] = 1,
			["item9"] = 1,
			["item10"] = 1,
			["item11"] = 1,
			["item12"] = 1,
		}
		datastore:SetAsync("sellvalueslist", sellvalueslist)
	end
end)

pcalls don’t work like that. The values it returns are whether the function succeeded or not (boolean) and the data that was returned inside. If it fails, the second value will instead be the error message. Since you named the first argument oldvalue, it becomes a boolean and gives the error you mentioned. Here’s a fix:

local success, oldvalue = pcall(function() -- first value: boolean
		return datastore:GetAsync("sellvalueslist")
	end)
	if success then -- if the pcall succeeded
		print("OLDVALUE")
		for i = 1, #images do
			if type(oldvalue) == "table" then
				print("IT IS TABLE")
			end
			print("Checking image", i, boardimage, images[i])
			if boardimage == images[i] then
				print("Found matching image")
				local gemprice3 = gemprice + oldvalue[i]
				local gemprice2 = gemprice / 2
				print("Gemprice values:", gemprice, gemprice2, gemprice3)
				sellvalueslist[i] = gemprice2
				print("Updated sellvalueslist", sellvalueslist[i])
				break
			end
		end
		datastore:SetAsync("sellvalueslist", sellvalueslist)
	else -- if it failed
		warn(oldvalue) -- will now become the error message
		sellvalueslist = {
			["item1"] = 1,
			["item2"] = 1,
			["item3"] = 1,
			["item4"] = 1,
			["item5"] = 1,
			["item6"] = 1,
			["item7"] = 1,
			["item8"] = 1,
			["item9"] = 1,
			["item10"] = 1,
			["item11"] = 1,
			["item12"] = 1,
		}
		datastore:SetAsync("sellvalueslist", sellvalueslist)
	end
1 Like

Okey.

Now it shows this error:
ServerScriptService.boardscript:163: attempt to perform arithmetic (add) on string and nil - Server -

This line gives the error:
local gemprice3 = gemprice + oldvalue[i]

Why is it nil? I can fix the string problem by using tonumber() but I have no idea, why is it nil. It prints “IT IS TABLE” and i is a number.

You didn’t index this table with numbers, you can just add “item” before the number to access the value like this:

if boardimage == images[i] then
				print("Found matching image")
				local gemprice3 = gemprice + oldvalue["item"..i]
				local gemprice2 = gemprice / 2
				print("Gemprice values:", gemprice, gemprice2, gemprice3)
				sellvalueslist["item"..i] = gemprice2
				print("Updated sellvalueslist", sellvalueslist["item"..i])
				break
			end
1 Like

Thanks! It works now. Now I know better about the datastores :slight_smile:

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