Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Target is a nil value with 'for in pairs', why?

Line 40, Workspace.Tower.towerShow.shootEnemies:40: attempt to index local 'target' (a nil value)

I'm not very good at 'for in pairs' functions. Having trouble connecting and securing a target block...

local Tower = script.Parent
local towerHolder = Tower.Parent
local shotAmmo = towerHolder.shotAmmo
local towerHealth = towerHolder.towerHealth
local damage = towerHolder.towerDamage
local Time = towerHolder.towerTimeShoot
local Enemies = game.Workspace.Bricks.Attackers

function Fire()
	local ammo = Instance.new("Part",shotAmmo)
	ammo.Size = Vector3.new(.63,.63,.63)
	ammo.BrickColor = BrickColor.new("Black")
	ammo.CFrame = Tower.CFrame + Vector3.new(0,7.3,0)
	ammo.CanCollide = false
	ammo.Name = "Ammo"
	ammo.TopSurface = "Smooth"
	ammo.BottomSurface = "Smooth"
	
	for i,v in pairs(Tower.AmmoParameters:GetChildren()) do
		v.Parent.Damage.Value = damage.Value
		v:Clone().Parent = ammo
		--ammo.Follow.Disabled = true
	end
	
	game.Debris:AddItem(ammo,0.5)
end

while wait(1/Time.Value) do
	if towerHealth.Value <= 0 then --Tower health has depleted, game over
		break
	end
	
	local towerMag = towerHolder.towerMagnitude
	for i,v in pairs(Enemies:GetChildren()) do
		if v then -- if Enemies found then
			while wait() do
				repeat
				local target = v:FindFirstChildOfClass("Part")

				until target.Active.Value == true --Problem...
				break
			end
		end
	end
end

print("Game Over")

1 answer

✓ Accepted answer

You declare the variable target inside the loop's scope. So it can only be used inside it, meaning it will be nil outside of it.

To fix it, just put local target the line before the loop and remove the local keyword from the one inside the loop, like this (Also you might want to add that wait() into the loop like i did):

local Tower = script.Parent
local towerHolder = Tower.Parent
local shotAmmo = towerHolder.shotAmmo
local towerHealth = towerHolder.towerHealth
local damage = towerHolder.towerDamage
local Time = towerHolder.towerTimeShoot
local Enemies = game.Workspace.Bricks.Attackers

function Fire()
    local ammo = Instance.new("Part",shotAmmo)
    ammo.Size = Vector3.new(.63,.63,.63)
    ammo.BrickColor = BrickColor.new("Black")
    ammo.CFrame = Tower.CFrame + Vector3.new(0,7.3,0)
    ammo.CanCollide = false
    ammo.Name = "Ammo"
    ammo.TopSurface = "Smooth"
    ammo.BottomSurface = "Smooth"

    for i,v in pairs(Tower.AmmoParameters:GetChildren()) do
        v.Parent.Damage.Value = damage.Value
        v:Clone().Parent = ammo
        --ammo.Follow.Disabled = true
    end

    game.Debris:AddItem(ammo,0.5)
end

while wait(1/Time.Value) do
    if towerHealth.Value <= 0 then --Tower health has depleted, game over
        break
    end

    local towerMag = towerHolder.towerMagnitude
    for i,v in pairs(Enemies:GetChildren()) do
        if v then -- if Enemies found then
            while wait() do
                local target
                repeat wait()
                target = v:FindFirstChildOfClass("Part")

                until target ~= nil and target.Active.Value == true
                break
            end
        end
    end
end

print("Game Over")

1 comment

0

Didn't hit me that after "until" was outside of the loop. Thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.