Error with welding in a script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to make it so when you equip a tool, it weld’s both the first and second handle onto the arms.

  2. What is the issue? The issue is, after a few seconds it deletes itself

  3. What solutions have you tried so far? It used to be a serverscript, but I changed it to a local because of a different issue, and I’m getting these errors with it now.

local tool = script.Parent
local handle1 = tool.Handle1
local handle2 = tool.Handle2
local character

local function equipped()

	character = tool.Parent

	local rightArm = character["Right Arm"]
	local leftArm = character["Left Arm"]

	if not (rightArm:FindFirstChild("Weld1")) then
		local weld1 = Instance.new("WeldConstraint")
		weld1.Name = ("Weld1")
		weld1.Part0 = rightArm
		handle1.CFrame = rightArm.CFrame
		weld1.Part1 = handle1
		weld1.Parent = rightArm

	else
		local weld1 = rightArm.Weld1
		handle1.CFrame = rightArm.CFrame
		weld1.Part1 = handle1
	end

	local rightArm = character["Right Arm"]
	local leftArm = character["Left Arm"]

	if not (leftArm:FindFirstChild("Weld2")) then
		local weld2 = Instance.new("WeldConstraint")
		weld2.Name = ("Weld2")
		weld2.Part0 = leftArm
		handle2.CFrame = leftArm.CFrame
		weld2.Part1 = handle2
		weld2.Parent = leftArm

	else
		local weld2 = leftArm.Weld2
		handle2.CFrame = leftArm.CFrame
		weld2.Part1 = handle2
	end
end

local function unequipped()
	local rightArm = character["Right Arm"]
	local leftArm = character["Left Arm"]

	if (rightArm:FindFirstChild("Hitbox1")) then
		rightArm.Hitbox1.Transparency = 0
	end

	if (leftArm:FindFirstChild("Hitbox2")) then
		leftArm.Hitbox2.Transparency = 0
	end
end


tool.Equipped:Connect(equipped)
tool.Unequipped:Connect(unequipped)

Any help would be really appreciated, as I’m trying to make my own melee system :slight_smile:

You don’t seem to be destroying or reparenting the tool in this script, so the issue lies somewhere else

Is the tool inside StarterPack, or is being placed inside the player’s Backpack or StarterGear using a script?

It’s in the Starterpack, I’ve tried duplicating the tool and removing all the scripts except that local, but the issue still continues.

1 Like

Is the script provided complete, or is it only a section?

Also, do you have other scripts outside of the tool which might be interfering? Best way to test this is to go to a new baseplate and see if the tools works correctly there

The script provided is completed, yeah

Also in a baseplate its the same issue

In that case, sometimes writing the script in a different way fixes some issues, so try this:

local tool = script.Parent


local leftArm, rightArm
-- Using "Once" instead of "Connect" will only run the function once, then disconnects itself automatically, hence the name
tool.Equipped:Once(function()
	local character = tool.Parent
	leftArm = character["Left Arm"]
	rightArm = character["Right Arm"]

	local handle1 = tool.Handle1
	handle1.CFrame = rightArm.CFrame

	local handle2 = tool.Handle2
	handle2.CFrame = leftArm.CFrame

	local weld1 = Instance.new("WeldConstraint")
	weld1.Name = "Weld1"
	weld1.Part0 = rightArm
	weld1.Part1 = handle1
	weld1.Parent = rightArm

	local weld2 = Instance.new("WeldConstraint")
	weld2.Name = "Weld2"
	weld2.Part0 = leftArm
	weld2.Part1 = handle2
	weld2.Parent = leftArm
end)

local function setHitboxTransparency(arm, hitbox, transparency)
	if arm then
		local hitbox = arm:FindFirstChild(hitbox)

		if hitbox then
			hitbox.Transparency = transparency
		end
	end
end

tool.Unequipped:Connect(function()
	setHitboxTransparency(rightArm, "Hitbox1", 0)
	setHitboxTransparency(leftArm, "Hitbox2", 0)
end)

@killya64 I’ve edited the code to include setting the hitbox’s transparency :slight_smile::+1:

1 Like

This helped me, tysm!!! :slight_smile:

1 Like