Cant insert into table? (Error)

local function dataTableCreate(plr)

local datakey = plr.UserId
if mainData:GetAsync(datakey) then
data[datakey] = mainData:GetAsync(datakey)
elseif not mainData:GetAsync(datakey) then
data[datakey]={
		['XP'] = 0;
		['Prestige'] = 0;
		['Rubys'] = 0;
		['LastCrosshair'] = 0;
		['Knives'] = {};
		['Effects'] = {};
		['CodesRedeemed'] = {};
		['LastClan'] = {};
}
end
print(mainData:GetAsync(datakey))
end

local function nilData(plr)

local datakey = plr.UserId
if data[datakey] then
mainData:SetAsync(datakey,data[datakey])
print('dataset')
else
warn('datasavingerror')
end


if data[plr] then data[plr] = nil end

end

local function gameBindToClose()
	for i, plr in pairs(game.Players:GetChildren()) do
		pcall(function()
			mainData:SetAsync(plr.UserId,data[plr])
		end)
	end
	wait(5)
end

local function addKnife(player, knife)

local name=player.UserId
	if data[name] and data[name]['Knives'] then
		table.insert(data[name]['Knives'],knife)
			else if not data[name] or data[name]['Knives'] then
			warn('Cant give knife')
		end
	end
		
end

game.Players.PlayerAdded:Connect(function(plr)

dataTableCreate(plr)
wait(.5)
addKnife(plr,'test2')

end)

Error:

ServerScriptService.Serverside.Main:72: invalid argument #1 to 'insert' (table expected, got string)  -  Server - Main:72

Line 72:

table.insert(data[name]['Knives'],knife)

I’ve been stuck on this issue since last night and I cannot figure it out for the life of me? Why does the output say its getting a string when I’m clearly calling the ‘Knives’ table in data[plr] ??

Maybe try printing the data[name]["Knives"] as the data already saved might be invalid.

Tried that already, data is valid

Problem Solved, I hopped on a voice call with Vinceops and the issue was that he saved the value as a string, not an array.

1 Like
ServerScriptService.Serverside.Main:72: invalid argument #1 to 'insert' (table expected, got string)  -  Server - Main:72

Your data is a string, not a table, as shown in the error.

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