Attempt to index nil with "Position"

so im making a tower defense game, but when i saw the tower attack, it killed multiple enemies at once, i tried making it so it keeps killing the same enemy until its dead, but, i got the error:

Attempt to index nil with "Position"

here is the code that makes the tower attack:

					repeat
						wait(firerate)
						script.Parent.Parent.CFrame = CFrame.new(script.Parent.Parent.Position,v.PrimaryPart.Position)
						Attack(v)
					until not v

also, v is the enemy

I wrote a Tower defense game a while ago, here is the place file.
Bloon-Defense.rbxl (1.9 MB)

okay, ill read some of the code and try to understand it, im not the sharpest tool in the shed

No problem, I wrote this my first few weeks of lua. The code is pretty simple and easy to understand for the most part.

Does the enemy have a PrimaryPart? If not, then there’s your error.

the enemy does have a primarypart

In that case, could you print this:

script.Parent.Parent.Position

it printed -100.24189758301, 1.2999954223633, 53.911167144775

1 Like

And could you also print the enemy primary pos

it printed -82.728439331055, 1.0999999046326, 81

plus i put it in a different spot

A possible issue is that your code checks if v still exists, then there is a delay before it tries to get it’s position. Which means v could no longer exist after the delay, so it can’t get it’s position.

Instead, try placing the delay at the end of your repeat loop:

repeat
	script.Parent.Parent.CFrame = CFrame.new(script.Parent.Parent.Position,v.PrimaryPart.Position)
	Attack(v)
	wait(firerate) -- Delay at end here, then check if v still exists
until not v

it still printed the error though.

In that case, try some simple debugging

print(v ~= nil) -- prints true if v exists, false if not

Place this line before and inside your repeat loop, figure out where v doesn’t exist

Have an if statement after the wait. I think the enemy still exists when the wait is called but it is gone after the wait.

Can you show us the whole script?

it printed true one time from before the repeat and 3 times inside the repeat

here is the whole script:

local range = script.Parent.Configuration.Range.Value
local damage = script.Parent.Configuration.Damage.Value
local enemies = game.Workspace.Enemies
local idleAnim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.Idle)
local shootAnim = script.Parent.Humanoid.Animator:LoadAnimation(script.Parent.Shoot)
local firerate = script.Parent.Configuration.Firerate.Value
idleAnim:Play()

coroutine.wrap(function()
	while true do
		range = script.Parent.Configuration.Range.Value
		damage = script.Parent.Configuration.Damage.Value
		firerate = script.Parent.Configuration.Firerate.Value
		wait()
	end
end)()

function Attack(target)
	idleAnim:Stop()
	spawn(function()
		shootAnim:Play()
		target.Health.Value -= damage
		script.Pistol:Play()
		shootAnim.Stopped:Wait()
		idleAnim:Play()
	end)
end

function Look()
	for i,v in pairs(enemies:GetChildren()) do
		if v then
		if v:IsA("Model") then
				if v and (v.PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude <= range/1.5 and v.Dead.Value == false then
					print(v ~= nil) -- prints true if v exists, false if not
					repeat
						print(v ~= nil) -- prints true if v exists, false if not
						script.Parent.Parent.CFrame = CFrame.new(script.Parent.Parent.Position,v.PrimaryPart.Position)
						Attack(v)
						wait(firerate)
					until not v
				else
					--enemy is dead so no code should be run
			end
		end
	end
	end
end

while true do
	Look()
	wait(firerate)
end

nothing really happened, it still errored

So it prints true, when does it start printing false?

never, it never even prints false.