Expected ')' (to close

13:52:07.737 - ServerScriptService.SavingPlayersWeapons:54: Expected ')' (to close '(' at line 48), got 'inventory_folder'

When I add another parenthesis to the first “end)” success becomes undefined for some reason

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, error = pcall(player_data:UpdateAsync(key,function(prev)
if not success then
print(error) -- this print call will only run if success is false
else return tools
end
end)
		inventory_folder:Destroy()
	end)
print("done")

This usually means that you forgot to add another ), which belongs to the first end) to finish the pcall function.

2 Likes

When you say the first end do you mean the end after else return tools?

Yes, the first end with the ) after return tools.

1 Like

You are not using pcall correctly. You are calling :UpdateAsync and passing its result to pcall. You want to pcall the function itself.

local ok, data = pcall(player_data.UpdateAsync, player_data, key, function(prev)
    return tools
end)

Since a:b(...) is sugar for a.b(a, ...) we need to pass player_data as the first argument so the function is called on the player_data data store.

also thx for using my tutorial lol

4 Likes

So is this how I’m supposed to do it?

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 ok, data = pcall(player_data.UpdateAsync, player_data, key, function(prev)
if not ok then
print(error) -- this print call will only run if success is false
else return tools
end
end)
		inventory_folder:Destroy()
	end)
print("done")

You should get in the habit of formatting your code (you can use a tab or 4 spaces which is the most common indentation level), it helps you debug code easier as you can see which end belongs to which function/statement:

image

4 Likes

I believe your error is within this block. In the first line , you have 3 opening brackets and you close 2 of them. I think what you need to do is the following:

	local success, error = pcall(player_data:UpdateAsync(key,function(prev)) -- added another bracket here
if not success then
print(error) -- this print call will only run if success is false
else return tools
end
end)

But also, I’m not familiar with pcalls so I could be wrong

Looking off of what others are saying, and my experience with pcall, how about you do pcall(function() and just have everything run inside of there instead of having multiple parameters. This will still catch errors and be a lot easier in general.