Sword on hip broken

  1. What do you want to achieve?
    I want my katana put on hip

  2. What is the issue?
    It doesn’t work out

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I trued looking on dev forum an yt but no results

I parent the katana inside of StarterCharacterScripts because when I resetted character, the attack system will not work

ERROR: Attempt to index nil with Parent, Line 28

	if script.Parent.Parent.Name == "Backpack" then
		local char = script.Parent.Parent.Parent.Character

		if char then
			local UpperTorso = char:FindFirstChild("Torso")
			local Weapon = char:FindFirstChild(script.Parent.Name)
			if UpperTorso and not Weapon then
				local weaponOnLeg = Instance.new("Model")
				weaponOnLeg.Name = script.Parent.Name
				weaponOnLeg.Parent = char

				Handle = script.Parent.Handle:Clone()
				Handle.Name = "Handle"
				Handle.Parent = weaponOnLeg

				local legWeld = Instance.new("Weld")
				legWeld.Name = "WeldOnLeg"
				legWeld.Part0 = UpperTorso
				legWeld.Part1 = Handle
				legWeld.C0 = CFrame.new(-1, -1.42,0.5) -- 1, -1.42,0.5
				legWeld.C0 = legWeld.C0 * CFrame.fromEulerAnglesYXZ(math.rad(150), math.rad(-180), -20.5)
				legWeld.Parent = Handle
			end
		end
	else
		if Handle.Parent then
			Handle.Parent:Destroy()
		end
	end
end

You seem to be missing some of the script, there is one too many end statements in that code.

Anyway the error would indicate that “Handle” is nil (it’s nothing). You need to define it.

hmmmm what do you mean by that

Declare a variable for Handle contained within the scope of that if statement.

local Handle = ""
if Handle.Parent then
	Handle.Parent:Destroy()
end

Don’t use that, local Handle = "" should be something else but currently in your script Handle is nil at that point.

like this?

while task.wait() do
	local Handle = script.Parent.Handle:Clone()
	if script.Parent.Parent.Name == "Backpack" then
		
		local char = script.Parent.Parent.Parent.Character

		if char then
			local UpperTorso = char:FindFirstChild("Torso")
			local Weapon = char:FindFirstChild(script.Parent.Name)
			if UpperTorso and not Weapon then
				local weaponOnLeg = Instance.new("Model")
				weaponOnLeg.Name = "Model"
				weaponOnLeg.Parent = char
				
				Handle.Name = "Handle"
				Handle.Parent = weaponOnLeg

				local legWeld = Instance.new("Weld")
				legWeld.Name = "WeldOnLeg"
				legWeld.Part0 = UpperTorso
				legWeld.Part1 = Handle
				legWeld.C0 = CFrame.new(-1, -1.42,0.5) -- 1, -1.42,0.5
				legWeld.C0 = legWeld.C0 * CFrame.fromEulerAnglesYXZ(math.rad(150), math.rad(-180), -20.5)
				legWeld.Parent = Handle
			end
		end
	else
		if Handle.Parent then
			Handle.Parent:Destroy()
		end
	end
end

I haven’t combed through the entire script but yes, that way “Handle” will be accessible anywhere within the containing while task.wait() do loop.

hmmm seems like the sword are still there(on hip) when equipped

Won’t if script.Parent.Parent.Name == "Backpack" then always be true? Even if unequipped a tool still belongs to the player’s backpack.

https://developer.roblox.com/en-us/api-reference/event/Tool/Equipped
https://developer.roblox.com/en-us/api-reference/event/Tool/Unequipped

You’re better off using these events to detect when a tool is equipped/unequipped.

Also I just realised that when player joins the game, the katana automatically equipped.

so i think if script.Parent.Parent.Name == "Backpack" then like didnt detect the katana is in the player backpack

https://developer.roblox.com/en-us/api-reference/property/Tool/ManualActivationOnly

Set this property to true, it requires the tool to be manually equipped.

I already tried it out but still doesn’t work

I just figured it out

Also sorry for wasting your time man im really really sorry


Handle = nil

function onUnequipped()
	if script.Parent.Parent == workspace or script.Parent.Parent.className ~= "Backpack" then
		return
	end

	local char = script.Parent.Parent.Parent.Character
	if char ~= nil then
		local torso = char:findFirstChild("Torso")
		local tool = char:findFirstChild(script.Parent.Name)
		if torso ~= nil and tool == nil then
			local model = Instance.new("Model")
			model.Name = script.Parent.Name
			model.Parent = char

			handle = script.Parent.Handle:clone()
			handle.CanCollide = false
			handle.Name = script.Parent.Name
			handle.Parent = model

			local weld = Instance.new("Weld")
			weld.Name = "BackWeld"
			weld.Part0 = torso
			weld.Part1 = handle
			weld.C0 = CFrame.new(-1, -1.42,0.5)
			weld.C0 = weld.C0 * CFrame.fromEulerAnglesYXZ(math.rad(150), math.rad(-180), -20.5)
			weld.Parent = handle
		end
	end
end

script.Parent.Unequipped:connect(onUnequipped)

function onEquipped()
	if handle ~= nil then 
		handle.Parent:remove()
	end
end

script.Parent.Equipped:connect(onEquipped)

Looks like you added equipped & unequipped which is what I suggested, congrats.