Invalid argument #1 to 'clear' (table expected, got nil)

Hello! Im trying to make function that will wipe people’s data. I tried to print problem but it says that data is nil no clue why

problem on 155 line

Module script:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
local Abbreviations = {K = 4}
local cansave={}

--[[function AbbreviateCash(Input)
	Input = tostring(Input) local abbreviation local _digits
	for abs,digits in pairs(Abbreviations) do 
		if #Input >= digits and #Input < (digits+3) then abbreviation = abs _digits = digits break end 
	end if not abbreviation then return "$" .. Input end
	local rounded = math.floor(Input/10^(_digits-2)) * 10 ^ (_digits-2)
	return "$" .. string.format("%.1f",rounded/10^(_digits-1)) .. abbreviation
end]]

local function LeaderBoardSetup(value)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local money = Instance.new("StringValue")
	money.Name = "ET Coin(s)"
	money.Value = value
	money.Parent = leaderstats
	
	return leaderstats
end

local function LoadData(player)
	local success, result = pcall(function()
		return PlayerData:GetAsync(player.UserId)
	end)
	
	if result and not success then
		player:Kick("your data got issues, " .. tostring(result))
	end
	return success, result
end

local function SavaData(player, data)
	if cansave[player] then
		local succes, result = pcall(function()
			PlayerData:SetAsync(player.UserId, data)
		end)
		if not succes then
			warn(result)
		end
		return succes
	end
end

local sessionData = {}

local playerAdded = Instance.new("BindableEvent")
local playerRemoving = Instance.new("BindableEvent")

local PlayerManager = {}

PlayerManager.PlayerAdded = playerAdded.Event
PlayerManager.PlayerRemoving = playerRemoving.Event

function PlayerManager.Start()
	for _, player in pairs(Players:GetPlayers()) do
		coroutine.wrap(PlayerManager.OnPlayerAdded)(player)
	end
	
	Players.PlayerAdded:Connect(PlayerManager.OnPlayerAdded)
	Players.PlayerRemoving:Connect(PlayerManager.OnPlayerRemoving)
	
	game:BindToClose(PlayerManager.OnClose)
end

function PlayerManager.OnPlayerAdded(player)
	player.CharacterAdded:Connect(function(character)
		PlayerManager.OnCharacterAdded(player, character)
	end)
	
	local succes, data = LoadData(player)
	sessionData[player.UserId] = succes and data or {
		Money = 0,
		UnlockIds = {}
	}
	
	local leaderstats = LeaderBoardSetup(PlayerManager.GetMoney(player))
	leaderstats.Parent = player
	
	playerAdded:Fire(player)
	local huh = coroutine.wrap(function()
		while wait(30) do
			if Players:FindFirstChild(player.Name) ~= nil then
				if cansave[player] == true then
					SavaData(player)
				else
					cansave[player] = true
					SavaData(player)
				end
			else
				coroutine.close()
				break
			end
		end
	end)()
end

function PlayerManager.OnCharacterAdded(player, character)
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Died:Connect(function()
			wait(3)
			player:LoadCharacter()
		end)
	end
end

function PlayerManager.GetMoney(player)
	return sessionData[player.UserId].Money
end

function PlayerManager.SetMoney(player, value)
	if value then
		sessionData[player.UserId].Money = value
		
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local money = leaderstats:FindFirstChild("ET Coin(s)")
			if money then
				money.Value = value --AbbreviateCash(value)
			end
		end
	end
end

function PlayerManager.GetUnlockIds(player)
	return sessionData[player.UserId].UnlockIds
end

function PlayerManager.AddUnlockId(player, id)
	local data = sessionData[player.UserId]
	
	if not table.find(data.UnlockIds, id) then
		table.insert(data.UnlockIds, id)
	end
end

function PlayerManager.RemoveUnlockId(player,id)
	local data = sessionData[player.UserId]
	for _, sus in ipairs(data) do
		if sus == id then
			table.remove(data ,_)
		end
	end
end

function PlayerManager.WipeData(player)
	local data = sessionData[player.UserId]
	table.clear(data)
	PlayerManager.SetMoney(player, 0)
end

function PlayerManager.OnPlayerRemoving(player)
	SavaData(player, sessionData[player.UserId])
	playerRemoving:Fire(player)
end

function PlayerManager.OnClose()
	if game:GetService("RunService"):IsStudio() then return end
	
	for _, player in ipairs(Players:GetPlayers()) do
		PlayerManager.OnPlayerRemoving(player)
	end
end

return PlayerManager
1 Like

You should try to print data to see what it is. It could help you and others better understand the issue. :smiley:

i tried it and says nil (problem on function WipeData)

local data = sessionData[player.UserId]
must be returning nil, meaning there is no value associated with player.UserId in sessionData

do you have any solutions how to fix that? Just asking

You can add a if statement and check if that playerId has any data

local data
if sessionData[player.UserId] then
data = sessionData[player.UserId]
else
return
end

or

local data = sessionData[player.UserId]
if data ~= nil then
-- Code
else
return
end

For some reason it still doing to nil

Mmm, well it doesn’t fix the nil problem. It just checks if it comes up as nil.

The only reason the data would come up as nil is if the is player.UserId is not defined in the table. So either A. The the players Id has not yet been set in the sessionData or B. the table it self is empty

To bug test I would, Print(sessionData). The whole table to see what is actually in the table

local data = sessionData[player.UserId]
if data ~= nil then
-- Code
else
print(sessionData)
return
end

^ something like that

Here’s my solution

function PlayerManager.WipeData(player, data) -- Whatever is wiping the players data, just send there data to the function
	
	table.clear(data)
	PlayerManager.SetMoney(player, 0)
end
-- For example
PlayerManger.WipData(player, sessionData[player.UserId])

ohh. now problem is invalid argument #1 to ‘clear’ (table expected, got Instance)

pretty certain that since you cleared the table, that would remove the Money, etc. values right? meaning the next line that comes right after it would trigger a function that would be trying to assign a nonexistent value in the table to a number

yeah, it should remove money and ids button from data player
But I don’t understand what the problem is

since table.clear pretty much wipes out the entire values as in removing them, i likely believe that that line is the only issue and that you should try running it without that line in the first place

It turned out that it wasn’t Player at all. now problem is solved
sorry for waste time ):

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