Sword Equip Welding issue

I am currently attempting to make a sword system, I’ve gotten some basics down such as welding the sword to the player but that’s also how my issue starts - I am able to equip the sword with the assigned keycode using UIS however i am unable to unequip it, I haven’t been on roblox nor programmed in Lua in almost 2 months in which my skills need repolishing however i am stumped at this issue apologies if my formatting is somewhat weird

Clarification: These are models (Not Tools)

Serverscriptservice:
Sword Equip/Dequip

local rs = game:GetService("ReplicatedStorage")
local sword = rs:WaitForChild("Sword")

local cframes = {
	["Right Arm"] = CFrame.new(0,-1,0)
}


sword.OnServerEvent:Connect(function(Player, isEquipped)
	
	local Character = Player.Character
	local Humanoid = Character.Humanoid
	if isEquipped then
		
		local Katana = Character:FindFirstChild("Katana")
		if Katana then
			local WeldH = Katana.PrimaryPart:FindFirstChild("SideWeld")
			if WeldH then
				WeldH:Destroy()
				Katana:SetPrimaryPartCFrame(Character:WaitForChild("Right Arm").CFrame * cframes["Right Arm"])
				local HandWeld = Instance.new("ManualWeld")
				HandWeld.Name = "HandWeld"
				HandWeld.Part0 = Katana.PrimaryPart
				HandWeld.Part1 = Character:WaitForChild("Right Arm")
				HandWeld.C0 = HandWeld.Part0.CFrame:ToObjectSpace(HandWeld.Part1.CFrame)
				HandWeld.Parent = HandWeld.Part0
			end
		elseif not isEquipped then
			local Katana = Character:FindFirstChild("Katana")
			if Katana then
				local HandWeld = Katana.PrimaryPart:FindFirstChild("HandWeld")
				if HandWeld then
					HandWeld:Destroy()
					
					local Case = Character:FindFirstChild("Case")
					if Case then
						Case:Destroy()
						Katana:SetPrimaryPartCFrame(Case.Case.CFrame * CFrame.new(0,-.125,2.60))
						Katana.Parent = Character
						
						local weld2 = Instance.new("ManualWeld")
						weld2.Name = "SideWeld"
						weld2.Part0 = Katana.PrimaryPart
						weld2.Part1 = Case.PrimaryPart
						weld2.C0 = weld2.Part0.CFrame:ToObjectSpace(weld2.Part1.CFrame)
						weld2.Parent = weld2.Part0
			
			
			
				end
			
		
			
					end				
				end
			end
		end
	end)

Sword Spawn Script

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Case = script.Case:Clone()
		Case:SetPrimaryPartCFrame(Character.HumanoidRootPart.CFrame)
		Case.Parent = Character
		
		local weld = Instance.new("ManualWeld")
		weld.Part0 = Case.PrimaryPart
		weld.Part1 = Character.PrimaryPart
		weld.C0 = weld.Part0.CFrame:ToObjectSpace(weld.Part1.CFrame)
		weld.Parent = weld.Part0
		
		local Sword = script.Katana:Clone()
		Sword:SetPrimaryPartCFrame(Case.Case.CFrame * CFrame.new(0,-.125,2.60))
		Sword.Parent = Character
		
		local weld2 = Instance.new("ManualWeld")
		weld2.Name = "SideWeld"
		weld2.Part0 = Sword.PrimaryPart
		weld2.Part1 = Case.PrimaryPart
		weld2.C0 = weld2.Part0.CFrame:ToObjectSpace(weld2.Part1.CFrame)
		weld2.Parent = weld2.Part0
		
		
	end)
end)

Localscript:

local rs = game:GetService("ReplicatedStorage")
local Sword = rs:WaitForChild("Sword")
local UIS = game:GetService("UserInputService")
local isEquipped = false
local debounce = false
local cooldown = 1


UIS.InputBegan:Connect(function(input,isTyping)
	if not isTyping then
		if input.KeyCode == Enum.KeyCode.F then
			if not isEquipped and not debounce then
				isEquipped = true

				Sword:FireServer(isEquipped)
			elseif isEquipped then
				--debounce = true
				isEquipped = false

				Sword:FireServer(isEquipped)
			end
		end
	end
end)

I have attempted at debugging it however there’s not much progress its way harder to work with especially when no errors are thrown so i’m unable to figure out exactly the issue

From a first glance I’ve noticed a logic issue here.

if isEquipped then

	local Katana = Character:FindFirstChild("Katana")
	if Katana then
		local WeldH = Katana.PrimaryPart:FindFirstChild("SideWeld")
		if WeldH then
			WeldH:Destroy()
			Katana:SetPrimaryPartCFrame(Character:WaitForChild("Right Arm").CFrame * cframes["Right Arm"])
			local HandWeld = Instance.new("ManualWeld")
			HandWeld.Name = "HandWeld"
			HandWeld.Part0 = Katana.PrimaryPart
			HandWeld.Part1 = Character:WaitForChild("Right Arm")
			HandWeld.C0 = HandWeld.Part0.CFrame:ToObjectSpace(HandWeld.Part1.CFrame)
			HandWeld.Parent = HandWeld.Part0
		end
	elseif not isEquipped then

elseif not isEquipped is only checked if isEquipped passes its check, as these conditions are mutually exclusive the former is always going to evaluate to false if the latter evaluates to true, if the latter evaluates to false the former is never evaluated.

I assume you’re missing an ‘end’ statement in the if Katana then block.

2 Likes

This is a lesson for me to not program in the middle of the night as i make such clear mistakes, I instantly fixed it as you opened my eyes Thanks.