DataStore2 just crapping itself

I’m having trouble whit DataStore2 for some reason when i try to add somthing in the DataStore2 table it gives me a error

ServerStorage.DataStore2:566: DataStore2() API call expected {string dataStoreName, Instance player}, got {string, nil} 

i tried everything but it still wont work it says a error at line 24 of the script

here is the script :

local ItemModule = game.ReplicatedStorage.Itemdata
local Itemdata = require(ItemModule)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local DataStore2 = require(ServerStorage:WaitForChild("DataStore2"))

waitTimer = 0.06
function randomitem(player)
	local ROLL = script.Parent.Parent.Rolls
	ROLL.WonItem.Visible = false
	ROLL.Visible = true
	for i = 1,20 do
		wait(waitTimer)
		waitTimer = waitTimer + 0.03
		local picked = (Itemdata.data[math.random(1, #Itemdata.data)])
		ROLL.RollImage.Image = "rbxassetid://"..picked.ImageID
		if i == 20 then
			ROLL.WonItem.Visible = true
			ROLL.WonItem.Text = "U got : ".. picked.Name
			local itemId = picked.ItemID
			print(itemId)

			local inventoryStore = DataStore2("Inventory", player)
			inventoryStore:Update(function(currentTable)
				if (currentTable[itemId]) then
					currentTable[itemId] = currentTable[itemId] + 1
				else
					currentTable[itemId] = 1
				end

				return currentTable
			end)
		end
	end
	waitTimer = 0.06
end
script.Parent.MouseButton1Click:Connect(randomitem)

i need help pls

MouseButton1Click doesn’t pass any player in it. You are better off using local scripts and remote events.

did it i made progress thanks for that but now it says

Players.Qu1cZ.PlayerGui.ScreenGui.Rolls.Script:26: attempt to index nil with number

currentTable is nil. Does the function passed to Update take an argument? Or is the argument nil because you haven’t set it before? Check that it’s not nil before you do stuff with it.

its not nil because it prints the number 5 because how i did this system was set items on ItemId so when i change a item name there is no data lose for players

If this is line 26, then currentTable is nil. I don’t really know though since you forgot to tell us which line is 26.

oops sorry yeah that is line 26 and itemId is connected to a local where it hase the itemid value

local itemId = picked.ItemID

Then currentTable is nil. Or your can print currentTable to show that I’m wrong, but I’m fairly confident here. I don’t use DS2 so I couldn’t help, but I do think that you might need to add a default value to save in case currentTable is nil.

yes you are right when u said {Or your can print currentTable} it printed nil while it is suppose to print data

Do you already have data saved? I would imagine that if this is the first time saving data, currentTable might be nil since there is no existing data.

the MouseButton1Click don’t return a player but nil [maybe because it is a localscript??], because the error says “got string, nil”.

already did it. it worked but now it says it cant find stuff

ServerScriptService.CaseManager:28: attempt to index nil with 'Hat' 

you were right it print nil whit data even

it says “index nil” so the parent of the “hat” is undefined or nil

CaseManager is a different script, isn’t it?

changed the name because it was called script

here is this script

if (currentTable[itemId]) then
	currentTable[itemId] = currentTable[itemId] + 1
else
	currentTable[itemId] = 1
end

as you can see it should not stop and go to else but it doesnt do it

then “currentTable” is nil, because you’re indexing it with [itemId]

umm yeah eh eh eh how could i say this umm there was a miss typo in the datastore2 and thats why it wasnt saving the whole time eh eh oopsie sorry for wasting your guys time