Invalid argument #1 to 'pairs' (table expected, got number)

Hello everyone i am using data stores 2. My other data store that stores stats is working perfectly, its just this other one i made. I don’t see what could be wrong because i copied my previous scripts’ code and changed the names and values a bit so i cant see why this wouldn’t be saving unless im using 2 of the same data stores at the same time and its overriding in a way… I looked up this error but i cant seem to find any answers,

script:


local DataStores2 = require(1936396537)
local TweenService = game:GetService("TweenService")
local debris = game:GetService("Debris")

DataStores2.Combine("CSludgeHead","USludgeHead","RSludgeHead","PSludgeHead"
	
	
	
	
	
	)	


local function PlayerAdded(plr)
	local dataCSludgeHead = DataStores2("CSludgeHead",plr)
	local dataUSludgeHead = DataStores2("USludgeHead",plr)
	local dataRSludgeHead = DataStores2("RSludgeHead",plr)
	local dataPSludgeHead = DataStores2("PSludgeHead",plr)

	-----------------------Values-------------------------------
	
	local Inventory = Instance.new("Folder", plr)
	Inventory.Name = "Inventory"
	
	local Gear = Instance.new("Folder", Inventory)
	Gear.Name = "Gear"
	
	--Katana--
	
	local Katana = Instance.new("IntValue", Gear)
	Katana.Name = "Katana"
	Katana.Value = 0

	--SludgeHead

	local CSludgeHead = Instance.new("IntValue", Gear)
	CSludgeHead.Name = "CSludgeHead"
	CSludgeHead.Value = 0

	local USludgeHead = Instance.new("IntValue", Gear)
	USludgeHead.Name = "USludgeHead"
	USludgeHead.Value = 0


	local RSludgeHead = Instance.new("IntValue", Gear)
	RSludgeHead.Name = "RSludgeHead"
	RSludgeHead.Value = 0


	local PSludgeHead = Instance.new("IntValue", Gear)
	PSludgeHead.Name = "PSludgeHead"
	PSludgeHead.Value = 0

	-------------------------LoadData------------------------
	
	--SlugeHead
	
	if dataCSludgeHead:Get() ~= nil then
		CSludgeHead.Value = dataCSludgeHead:Get()
	else
		CSludgeHead.Value = 0
	end

	if dataUSludgeHead:Get() ~= nil then
		USludgeHead.Value = dataUSludgeHead:Get()
	else
		USludgeHead.Value = 0
	end

	if dataRSludgeHead:Get() ~= nil then
		RSludgeHead.Value = dataRSludgeHead:Get()
	else
		RSludgeHead.Value = 0
	end
	
	if dataPSludgeHead:Get() ~= nil then
		PSludgeHead.Value = dataPSludgeHead:Get()
	else
		PSludgeHead.Value = 0
	end
	
	----------------------Save Data---------------------------
	
	--SludeHead
	
	CSludgeHead.Changed:Connect(function()
		dataCSludgeHead:Set(CSludgeHead.Value)
		print("CSludgeHead Has Been Changed")
	end)

	USludgeHead.Changed:Connect(function()
		dataUSludgeHead:Set(USludgeHead.Value)
	end)

	RSludgeHead.Changed:Connect(function()
		dataRSludgeHead:Set(RSludgeHead.Value)
	end)

	PSludgeHead.Changed:Connect(function()
		dataPSludgeHead:Set(PSludgeHead.Value)
	end)
	
end

-- fire for already joined players
for _,Player in pairs(game.Players:GetPlayers()) do
	coroutine.wrap(function() PlayerAdded(Player) end)()
end

game.Players.PlayerAdded:connect(PlayerAdded)

That error means you were trying to do a pairs loop with a number instead of table.

local _table = {}
local number = 0

for index, value in pairs(_table) do
   -- will not error
end 

for index, value in pairs(number) do
   -- will error
end

What line is the error occurring on?

1 Like

Error on BeforeSave: Model.MainModule:573: invalid argument #1 to ‘pairs’ (table expected, got number)

module script at line 573. Im using data stores 2 module script so it has to be something wrong with my script using it

I don’t use DataStore2 so I wouldn’t be able to help you, but you can try to see if you incorrectly wrote something:

1 Like

Someone correct me if I’m wrong, but I believe the issue is probably because when you combine with DataStore2, the first parameter is always “DATA”. So it would have to be

DataStores2.Combine("DATA", "CSludgeHead","USludgeHead","RSludgeHead","PSludgeHead")

And not how you did it in your code, as you forgot to provide the DATA string

1 Like

ill try that and see if it will work

Alright, if that isn’t the solution then I’ll try to keep helping out as to what it might be to the best of my ability

1 Like

okay it seems to have worked ive tried twice, my other data store doesnt have that at the start so i guess this data store was colliding. Im guessing that helps define diffrent data stores that are used at the same time anyways thank you dude you really helped a lot

Nvm it does lol it was only this one who didnt have it at the start

Glad to have helped you! If you require a bit more assistance with how DataStore2 works, I recommend you check out the Official Documentation, in fact, this specific issue mentioned is mentioned in the Gotchas section

1 Like