Weld is not removing

The script can create weld but cant remove weld

local weldi = part:FindFirstChild("WeldConstraint")
	if weldi ~= nil then
		weldi:Remove()
	end
	if weldi ~= not nil then
		local char = player.Character or player.CharacterAdded:Wait()
		local weld = Instance.new("WeldConstraint")
		weld.Parent = part
		weld.Part0 = part
		weld.Part1 = char.HumanoidRootPart
        end
1 Like

Obviously the weld constraint was created and the first if condition did not pass(it was nil). Then if it wasn’t found, a weld would be created. Logically from top to bottom.

Some other notes:

  • Instance:Remove() is deprecated, use Instance:Destroy()
  • Is this code placed in a function?
2 Likes

Instance:Destroy() could not worked either and yeah the code is a part of a function

Try not use any incomprehensive statements, it translates to the same thing as weldi ~= true.

A complete solution is easy, I simply used the else statement for cases if the weld is not found.

local weld = part:FindFirstChild("WeldConstraint")

if weld then
	weld:Destroy()
else
	local char = player.Character or player.CharacterAdded:Wait()
	weld = Instance.new("WeldConstraint")
	weld.Parent = part
	weld.Part0 = part
	weld.Part1 = char.HumanoidRootPart
end
1 Like

it worked and thanks for the advice

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