"Attempt to index nil with 'PrimaryPart'." Issue

Hello Developers!

I’m having an issue with my clone potion. The issue is that after killing an humanoid, it will not target them after respawning (Assuming there are no other NPCS that are still existing) due to the error seen in the title

local Tool = script.Parent
local DrinkEvent = Tool:WaitForChild("Drinking")
--//Services\\--
local Plrs = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local Deb = game:GetService("Debris")
local PFS = game:GetService("PathfindingService")
local RunS = game:GetService("RunService")
--//Messages\\--
local SpawnMessage = {
	"OHOHOOOoo-HAA-HA-HAAAAAAA..!",
	"HENLO",
	"ITZ KIL 0R B KIL'D.",
	"Legalizing nuclear bobms.",
	"Target Required.",
	"SAY GODBY 2 U'RE NECAPZ BUDY.",
	"B",
	"WHY D0 I LIV LIKE THIS?",
	"Science tells us that laughter is the best medicine. Put that theory to the test with the Schadenfreude taunt pack, now available in the Mann Co. Store, which gives all nine classes the ability to laugh at a corpse and see if it gets any better.",
	"INCOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOMINNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",
	"my finger hurts after typing this.",
	"I'm only a \"Human\" after all."
}
local DyingMessages = {
	"DID, DID I GOT HIM?!",
	"OW",
	"不",
	"STOP",
	"Demoman_Dies_After_Getting_Kill_With_StickyJumper.wav",
	"KILLFED OR SOMETHING, SRRY I DON'T KNOW",
	"One must imagine me dying happily ever after.",
	"NO"
}
--//Connections\\--
local function LungeAttack(PlrClone)
	PlrClone.Humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
	PlrClone.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp,false)
	
	local Vectorforce = Instance.new("VectorForce",PlrClone.HumanoidRootPart)
	local Attachment = Instance.new("Attachment",PlrClone.HumanoidRootPart)
	Vectorforce.Attachment0 = Attachment
	Vectorforce.Force = Vector3.new(0,200,-1000)
	Vectorforce.ApplyAtCenterOfMass = false
	Deb:AddItem(Vectorforce,0.5)
	Deb:AddItem(Attachment,0.5)
	
	for index, Joints in pairs(PlrClone:GetDescendants()) do
		if Joints:IsA("Motor6D") then
			local BallSocket = Instance.new("BallSocketConstraint",Joints.Parent)
			local A1 = Instance.new("Attachment",Joints.Part0)
			local A2 = Instance.new("Attachment",Joints.Part1)
			BallSocket.Attachment0 = A1
			BallSocket.Attachment1 = A2
			A1.CFrame = Joints.C0
			A2.CFrame = Joints.C1
			BallSocket.LimitsEnabled = true
			BallSocket.TwistLimitsEnabled = true
			Joints:Destroy()
		end
	end
	
	local Explosion = Instance.new("Explosion")
	Deb:AddItem(Explosion,2)
	Explosion.Parent = workspace.Debris
	Explosion.Position = PlrClone.HumanoidRootPart.Position
	Explosion.DestroyJointRadiusPercent = 0
	
	local RandomChance = math.random(1,5)
	if RandomChance == 5 then
		game:GetService("Chat"):Chat(PlrClone.Head,DyingMessages[math.random(1, #DyingMessages)],Enum.ChatColor.Red)
	end
	
	local ExplosionSoundEffect = Instance.new("Sound")
	ExplosionSoundEffect.Parent = PlrClone.HumanoidRootPart
	ExplosionSoundEffect.SoundId = "rbxassetid://3802269741"
	ExplosionSoundEffect.RollOffMaxDistance = 70
	ExplosionSoundEffect.Volume = 0.5
	ExplosionSoundEffect:Play()
	
	local Hits = {}
	Explosion.Hit:Connect(function(OnHit)
		local Character = OnHit:FindFirstAncestorWhichIsA("Model")
		local Humanoid = Character and Character:FindFirstChildWhichIsA("Humanoid")
		if Humanoid and not Hits[Character] then
			Hits[Character] = true
			Humanoid:TakeDamage(33)
			task.wait(0.3)
			Hits[Character] = false
		end
	end)
	PlrClone.Humanoid.Health = 0
	Deb:AddItem(PlrClone,5)
end

local function FindNearestEnemy(CharClone, Character)
	local Enemies = {}
	local Nearest = math.huge
	local Target = nil

	for _, Enemy in pairs(workspace:GetChildren()) do
		local RootPart = Enemy:FindFirstChild("HumanoidRootPart")
		if RootPart then
			local Distance = (CharClone.PrimaryPart.Position - RootPart.Position)
			if Distance.Magnitude < Nearest and Enemy ~= Character then
				table.insert(Enemies,{
					Magnitude = Distance.Magnitude,
					enemy = Enemy
				})
			end
			for _, Entry in pairs(Enemies) do
				local Magnitude = Entry.Magnitude
				local enemy = Entry.enemy

				if Magnitude < Nearest then
					Nearest = Magnitude
					Target = enemy
				end
			end
			return Target
		end
	end
end

DrinkEvent.OnServerEvent:Connect(function(Plr)
	local Character = Plr.Character or Plr.CharacterAdded:Wait()
	Character.Archivable = true
	
	local CharClone = Character:Clone()
	CharClone.Parent = game.Workspace
	Deb:AddItem(CharClone,5)
	
	CharClone.Name = "Clone"
	CharClone.Humanoid.WalkSpeed = 23
	CharClone.Humanoid.MaxHealth = 88
	CharClone.Humanoid.Health = CharClone.Humanoid.MaxHealth
	CharClone:FindFirstChild("Animate"):Destroy()
	local RandomChance = math.random(1,5)
	if RandomChance == 5 then
		game:GetService("Chat"):Chat(CharClone.Head,SpawnMessage[math.random(1, #SpawnMessage)],Enum.ChatColor.Red)
	end
	
	local NewAnimate = script:WaitForChild("Animate"):Clone()
	NewAnimate.Parent = CharClone
	NewAnimate.Enabled = true
	CharClone.HumanoidRootPart.CFrame = CharClone.HumanoidRootPart.CFrame + Vector3.new(0, 0, -2)
	local IsLunging = false
	
	repeat
		local enemy = FindNearestEnemy(CharClone, Character)

		local Destination = enemy.PrimaryPart.Position
		local Beginning = CharClone.PrimaryPart.Position

		local Path = PFS:CreatePath()
		Path:ComputeAsync(Beginning,Destination)
		if Path.Status == Enum.PathStatus.Success then
			local Waypoints = Path:GetWaypoints()

			if Waypoints and Waypoints[2] then
				local Data = Waypoints[2]
				if Waypoints[2].Action == Enum.PathWaypointAction.Jump then
					CharClone.Humanoid.Jump = true
				end
				CharClone.Humanoid:MoveTo(Data.Position)
			end
		end
		local Distance = (Beginning - Destination).Magnitude
		if Distance < 5 and IsLunging == false and enemy:FindFirstChildWhichIsA("Humanoid").Health >= 1 then
			IsLunging = true
			local PlrClone = CharClone
			LungeAttack(PlrClone)
			task.wait(2)
			IsLunging = false
		end
		task.wait()
	until CharClone.Humanoid.Health == 0
end)

On your loop, you should try re-indexing your character, each times your remote event fires to the server, it sends a “Player” object, the character property of that object is not static, meaning that it can be deleted at any moment given, so your character will be equal to nil. What I suggest is that you redefine the character variable just like you did at the top inside of that loop.

repeat
Character = Plr.Character or Plr.CharacterAdded:Wait()
local enemy = FindNearestEnemy(CharClone, Character)
--// continue the rest of your code
until ...
1 Like

Doesn’t work sadly, still gives me that error.