Attempt to call a nil value

Hello there, I am getting an error message that says

“cloud_9038237548.Script:16: attempt to call a nil value”

I do not know much about scripting so making this script required me to do a lot of internet surfing mainly using the Roblox Developer Hub and the DevForum.

I am making this script for a Plugin so that I am able to remove all of the “Welds” and “Snaps” located in the workspace, the reason I cannot do this manually is that there is over 300 total which would take me forever to delete everyone.

This is what I am getting in my Output:

Script:

local toolbar = plugin:CreateToolbar("Remove Welds") -- Name
local PluginButton = toolbar:CreateButton("Remove Welds", "Removes all Welds located in the Workspace.", "rbxassetid://4989743062") -- Name : Desc : Icon
PluginButton.ClickableWhenViewportHidden = true



local function onPluginButtonClicked()
	print("Plugin Button Clicked") -- Print a message when Plugin button is clicked.
local classesToRemove = {
	["Weld"] = true,
	["Snap"] = true,
}

	for n, descendant in pairs(workspace:GetDescendants()) do
		if classesToRemove[descendant.ClassName] then
			classesToRemove:Destroy()
			print("All Welds located in Workspace has now been deleted.") -- Print a message when Welds are deleted.
		end
	end
end

PluginButton.Click:Connect(onPluginButtonClicked)

You’re calling :Destroy() on the classesToRemove table, instead of descendant.

for n, descendant in pairs(workspace:GetDescendants()) do
	if classesToRemove[descendant.ClassName] then
		descendant:Destroy()
		print("All Welds located in Workspace has now been deleted.") -- Print a message when Welds are deleted.
	end
end
1 Like

How would I go about fixing this?

I already gave you the code but you can fix it by changing classesToRemove:Destroy() to descendant:Destroy() to actually call :Destroy() on the descendant instance.

1 Like

Sorry about that, I never noticed any changes but reading over the script again I just saw it. Once again sorry, and also thank you very much for helping me fix this issue.