Code breaks after running it one time

  1. What do you want to achieve? Keep it simple and clear!
    Equipable/Unequipable Sword

  2. What is the issue? It only works one time, then it breaks and gives this error: image

And a video of issue: https://streamable.com/ry924f

  1. What solutions have you tried so far? I tried so many things but did not work

here is code:

local script that fires the event to equip/unequip

local UIS = game:GetService("UserInputService")
local Bool = game.ReplicatedStorage:WaitForChild("Equipped")
Bool = false
local equipped = false

UIS.InputBegan:Connect(function(input)
    
    if input.KeyCode == Enum.KeyCode.E then
        
    
        if equipped == false then
        equipped = true
        game.ReplicatedStorage.SwordEquipped:FireServer()
        print("true")
        else
        equipped = false
        game.ReplicatedStorage.SwordUnEquipped:FireServer()
        print("false")
        
        end
    end                
end)

server script that makes equip/unequip

local sword = game.ServerStorage.Sword:Clone()

    game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        
game.ReplicatedStorage.SwordEquipped.OnServerEvent:Connect(function()
       
        sword.Parent = character    
        local handle = sword:WaitForChild("WeldPart")
        
        
        local Weld = Instance.new("Weld", sword)
        Weld.Name = "Weld"
        Weld.Part0 = handle
            Weld.Part1 = character["Right Arm"]
            end)
        
game.ReplicatedStorage.SwordUnEquipped.OnServerEvent:Connect(function()
                
            sword:Destroy()
            
        end)
    end)
end)

I hope someone can help me

I belive it is this line here

When the tool is equipped the parent changes to the character. This property is locked so will error when you attempt to change it, add – before that line and give it a go. Hope this helps.

1 Like

what do you mean, what should i add?
edit: I got it, it worked, thanks :smiley: