Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Cannot delete weld coming back with nil?

I'm trying to make something where if an object is touched it will weld that part to the main part. It works but when I run an event from a GUI to delete the weld it comes back as nil with this error code: attempt to index nil with 'Destroy'

Heres the script:

local main = script.Parent
local weld

script.Parent.Touched:Connect(function(plart)
	local other = plart --plart was a typo (edit)
	local weld = Instance.new("Weld", main)
	weld.C0 = main.CFrame:Inverse() * other.CFrame
	weld.Part0 = main
	weld.Part1 = other	
end)

game.ReplicatedStorage.DelWeld.OnServerEvent:Connect(function()
	weld:Destroy()
	script.Disabled = true	
end)	

If anyone could help me that would be appreciated.

Thank you.

1 answer

✓ Accepted answer

This is because you defined weld in a local scope, making it unavailable to the .OnServerEvent call. I noticed you have a pre-declared variable that I assume you tried to allocate the weld to for global manipulation. To fix this, just remove the local keyword on line 6.

This discussion is preserved as it was. New answers, comments, and votes are closed.