Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Table expected, got object?

I have a datastore script which is supposed to save the players stats and inventory. Why do I get the error: "20:07:45.026 - ServerScriptService.DSHandler:26: bad argument #1 to 'pairs' (table expected, got Object)"? The error occurs at line 26.

Here is the code:

statStore = game:GetService('DataStoreService'):GetDataStore('onepiheceaeikenicksicasaw5')
invStore = game:GetService('DataStoreService'):GetDataStore('inventorysicahiyoaeike3')
game.Players.PlayerAdded:connect(function(plr)
	-- OUR VARIABLES WEEEEEE
	local uniquekey = 'id-'..plr.userId
	local sup = Instance.new('Folder', plr)
	sup.Name = 'Data'
	local cash = Instance.new('IntValue')
	cash.Parent = sup
	cash.Name = 'Beli'
	local hamon = Instance.new('IntValue')
	hamon.Parent = sup
	hamon.Name = 'Fruit'
	local fac = Instance.new('IntValue')
	fac.Parent = sup
	fac.Name = 'Faction'
	local dmg = Instance.new('IntValue')
	dmg.Parent = plr
	dmg.Name = 'dmg'
	local GetSaved = statStore:GetAsync(uniquekey)
	local inv = invStore:GetAsync(uniquekey)
	if GetSaved then
		hamon.Value = GetSaved[1]
		cash.Value = GetSaved[2]
		fac.Value = GetSaved[3]
		for _,tool in pairs(invStore) do
			local item = game.ServerStorage.Items:FindFirstChild(tool):Clone()
			item.Parent = plr.Backpack
		end
	else
		local NumbersForSaving = {hamon.Value,cash.Value,fac.Value}
		statStore:SetAsync(uniquekey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:connect(function(plr)
	local uniquekey = 'id-'..plr.userId
	local Savetable = {plr.Data.Fruit.Value,plr.Data.Beli.Value,plr.Data.Faction.Value}
	local inv = {}
	for _,v in pairs(plr.Backpack:GetChildren()) do
		if v:IsA("Tool") and v.Name ~= "Combat" then
			table.insert(inv,v.Name)
		end
	end
	statStore:SetAsync(uniquekey,Savetable)
	invStore:SetAsync(uniquekey,inv)
end)

1 answer

✓ Accepted answer

I answered your question via chat, but I'll answer it here too.

It seems that instead of using the table you defined, inv you used invStore which is the variable holding the Datastore, hence the object part of your error.

It's a simple fix of replacing invStore with inv on line 26.

This discussion is preserved as it was. New answers, comments, and votes are closed.