Variable is Undefined

You can’t see it, but in studio the second success is underlined in blue (which I’m pretty sure means it’s undefined). However in the output there are no errors saying that success is undefined, so does that mean that it’s not even going through that line of code. Thanks

Players.PlayerRemoving:Connect(function(client)
	print("PlayerLeft")
	local key = "client_".. client.UserId
	local tools = {}
	local inventory_folder = inventories[client.Name]
	
	for _, item in pairs(inventory_folder:GetChildren()) do
		table.insert(tools, item.Name)
	end
	
	local success, data = 
		pcall(player_data.UpdateAsync, player_data, key, function(prev)
			if not success then ---Success is underlined in blue
				print(error) -- this print call will only run if success is false
			else 
				return tools
			end
		end)
	inventory_folder:Destroy()
end)

I think the success must be outside the function. (I DON’T KNOW IF THIS IS CORRECT)

local success, data = 
	pcall(player_data.UpdateAsync, player_data, key, function(prev)

		end)
if not success then ---Success is underlined in blue
	print(error) -- this print call will only run if success is false
else 
	return tools
end
	inventory_folder:Destroy()
2 Likes

The reason it doesn’t error is because you are essentially putting in the statement:

if not nil then
    print("Test") -- prints out
end

It’s undefined because you can’t define local variables inside them self.
its the same thing as:

local hi = function()
	if not hi then  -- "hi" is undefined
		print("Hi") -- prints
	end
end

hi()
1 Like