PrimaryPart is not valid member of backpack

Sorry For Bad English, I Am Making Lasso System For My Western Game, And If Player Hit On Lasso, It Instantly Teleport Player To Specfic Place, The Lasso Itself Work Good, But Problem Is That When I Quickly Equip Other Tool After I Activate Lasso Tool, The Outputs Says “PrimaryPart is not valid member of backpack”, How Do I Fix This?, I Am Begginer At Scripting.

local Tool = script.Parent
local Handle = Tool.Handle
local MouseLoc = Tool:WaitForChild("MouseLoc",10)

Tool.Enabled = true

function fire(direction)
	
	Tool.Enabled = false
	
	local anim = Instance.new("Animation")
	anim.Name = "ThrowAnim"
	anim.AnimationId = "rbxassetid://13181333852"
	local track
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
	track.Priority = Enum.AnimationPriority.Movement
	track.Looped = true
	track:Play()
	
	task.wait(0.8)

	local vCharacter = Tool.Parent
	local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)
	
	Handle.Transparency = 1
	
	local lasso = Instance.new("Part")  
	local spawnPos = vCharacter.PrimaryPart.Position
	spawnPos  = spawnPos + (direction * 5)	
	lasso.Position = spawnPos
	lasso.Size = Vector3.new(2,2,2)
	lasso.Velocity = direction * 200
	lasso.BrickColor = BrickColor.new('Cashmere')
	lasso.Shape = 0
	lasso.Locked = true
	lasso.BottomSurface = 0
	lasso.TopSurface = 0
	lasso.Name = ""
	lasso.Elasticity = 1
	lasso.Reflectance = .2
	lasso.Friction = 0
	lasso.Orientation = Vector3.new(0, -90, 0)
	
	local A1 = Instance.new("Attachment", vCharacter["Right Arm"])
	local A2 = Instance.new("Attachment", lasso)
	
	local lineForce = Instance.new("LineForce",lasso)
	lineForce.Attachment0 = A2
	lineForce.Attachment1 = A1
	lineForce.Magnitude = 200
	
	local rotation = Instance.new("BodyAngularVelocity", lasso)
	rotation.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
	rotation.AngularVelocity = Vector3.new(0,30,0)
	
	local spring = Instance.new("SpringConstraint", lasso)
	spring.Attachment0 = A1
	spring.Attachment1 = A2
	spring.Visible = true
	spring.Coils = 0
	spring.Color = lasso.BrickColor
	
	local scriptt = game.ServerStorage.Lasso:Clone()
	scriptt.Parent = lasso
	
	Handle.Mesh:Clone().Parent = lasso
	
	Handle.FlyingSound:Clone().Parent = lasso
	
	local new_script = script.Parent.CannonBall:Clone()
	new_script.Disabled = false
	new_script.Parent = lasso

	local creator_tag = Instance.new("ObjectValue")
	creator_tag.Value = vPlayer
	creator_tag.Name = "creator"
	creator_tag.Parent = lasso

	lasso.Parent = workspace
	
	task.wait(2.5)
	lasso:Destroy()
	Handle.Transparency = 0
	Tool.Enabled = true
end



function onActivated()
	if not Tool.Enabled then
		return
	end
	local character = Tool.Parent;
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid then
		print("Humanoid not found")
		return 
	end
	local targetPos = MouseLoc:InvokeClient(game:GetService("Players"):GetPlayerFromCharacter(character))
	local lookAt = (targetPos - character.Head.Position).unit
	fire(lookAt)
end


Tool.Activated:Connect(onActivated)

To debug this kind of thing add print() statements to check what variables are, e.g
print(vCharacter)
My guess here is that the tool has been moved from the character model to the backpack.

The issue is that when you equip another tool, the tool’s parent changes from your character to your backpack.

To fix your script, you can either:

Define the vCharacter variable at the top of your fire function, since when the tool is Activated its parent is the character.

Or

Define the character variable whenever the tool is equipped, outside of the activated function.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.