Buy tool not working

My buy tool script isn’t working because I made a non unequip script and when I buy a tool it doesn’t work. In the output it puts parent property is locked. How do I fix this?

-Buy script-

local guncost = script.Parent.Parent.GunCost.Value
local ammocost = script.Parent.Parent.AmmoCost.Value
local gun = game.ServerStorage.Guns:FindFirstChild(script.Parent.Parent.Gun.Value)
script.Parent.ProximityPrompt.ObjectText = script.Parent.Parent.Gun.Value

script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	if player.leaderstats.Points.Value > guncost then
		if player.Character:FindFirstChildOfClass("Tool").Name == "Primary" then
			player.Character.Primary:Destroy()
			local clonedgun = gun:Clone()
			clonedgun.Name = "Primary"
			clonedgun.Parent = player.Character
		end
		if player.Character:FindFirstChildOfClass("Tool").Name == "Secondary" then
			player.Character.Secondary:Destroy()
			local clonedgun = gun:Clone()
			clonedgun.Name = "Secondary"
			clonedgun.Parent = player.Character
		end
	end
end)

-Backpack Script-

local UIS = game:GetService("UserInputService")

local ContexActionService = game:GetService("ContextActionService")

local player = game.Players.LocalPlayer
local Backpack = player.Backpack

local gun1 = Backpack:FindFirstChild("Primary") 
local gun2 = Backpack:FindFirstChild("Secondary")

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

local Hasgun1 

local LastTick = tick()

task.wait(1)

local function OnInput(input, Chat)
	if Chat then return end 

	local GetKeyCode = input.KeyCode 
	if GetKeyCode == Enum.KeyCode.One and not Hasgun1 then 
		Hasgun1 = true
		Humanoid:EquipTool(gun1)
	elseif GetKeyCode == Enum.KeyCode.Two and Hasgun1 then
		Hasgun1 = false
		Humanoid:EquipTool(gun2)
	end
end
UIS.InputBegan:Connect(OnInput)
OnInput({KeyCode = Enum.KeyCode.One})

local function OnTypeInput(inputType, Chat)
	if Chat then return end 

	local CurrentTick = tick()
	if inputType.UserInputType == Enum.UserInputType.MouseWheel and CurrentTick - LastTick >= .8 then
		LastTick = CurrentTick
		Humanoid:EquipTool(Hasgun1 and gun2 or gun1)
		Hasgun1 = not Hasgun1
	end
end
UIS.InputChanged:Connect(OnTypeInput)

Can you tell me in wich line the error is located ?

I don’t exactly understand the issue you are describing. But, the line “player.Character.Primary:Destroy()” could be trying to destroy the HumanoidRootPart. Also, you should probably put the tool in the backpack and not the player’s character. Also, this code breaks if the player unequips the tool and it goes into the backpack. Because you are only checking if it’s in the character and not the backpack.