尝试使用“FindFirstChild”索引 nil

问题描述 投票:0回答:1

我正在尝试制作一个炮塔 Ai 功能,并且能够从一个名为 Bobby 的 npc 中生成。当炮塔生成时,它会说“尝试使用‘FindFirstChild’索引 nil”,这是代码。

local clone = script.Parent:Clone()

local human = script.Parent.Humanoid
local body = script.Parent.Head
local barrel = script.Parent.Barrel
local legs = script.Parent.Legs
local hinge = script.Parent.Torso

local aimer = script.Parent.Aimer
local aimAttach = legs.Aim

while hinge:GetNetworkOwnershipAuto() do
    wait()
    hinge:SetNetworkOwner(nil)
end

local settingsFolder = script.Parent.Settings

local barrelHinge = script.Parent.BarrelHinge

local aimPos = barrel.AimPoint
local barrelTip = body.BarrelTip
local barrelChamber = body.BarrelChamber
local shootSound = body.Shoot
local impact = body.Impact
local smoke = barrelTip.Smoke
local sparks = body.Capcitor.Sparks

--Lightweight Humanoid
human:SetStateEnabled(Enum.HumanoidStateType.Climbing,false)
human:SetStateEnabled(Enum.HumanoidStateType.FallingDown,false)
human:SetStateEnabled(Enum.HumanoidStateType.Flying,false)
human:SetStateEnabled(Enum.HumanoidStateType.Freefall,false)
human:SetStateEnabled(Enum.HumanoidStateType.GettingUp,false)
human:SetStateEnabled(Enum.HumanoidStateType.Jumping,false)
human:SetStateEnabled(Enum.HumanoidStateType.Landed,false)
human:SetStateEnabled(Enum.HumanoidStateType.Physics,false)
human:SetStateEnabled(Enum.HumanoidStateType.PlatformStanding,false)
human:SetStateEnabled(Enum.HumanoidStateType.Ragdoll,false)
human:SetStateEnabled(Enum.HumanoidStateType.Running,false)
human:SetStateEnabled(Enum.HumanoidStateType.RunningNoPhysics,false)
human:SetStateEnabled(Enum.HumanoidStateType.Seated,false)
human:SetStateEnabled(Enum.HumanoidStateType.StrafingNoPhysics,false)
human:SetStateEnabled(Enum.HumanoidStateType.Swimming,false)

local particleFlash = {barrelTip.Flash1,barrelTip.Flash2,barrelTip.Flash3}
local currentFlash = 1
local lightFlash = barrelTip.PointLight

local tween = game:GetService("TweenService")

local currentTarget = nil
local canSeeTarget = false

local event = Instance.new("BindableEvent")
function spawner(func,...)
    local connection = event.Event:Connect(func)
    event:Fire(...)
    connection:Disconnect()
end

function checkDist(target)
    return (body.Position - target.Position).Magnitude
end

function checkSight(target)
    local params = RaycastParams.new()
    params.FilterDescendantsInstances = {script.Parent}
    local hit = workspace:Raycast(aimPos.WorldPosition,(target.Position - aimPos.WorldPosition).Unit * settingsFolder.Range.Value, params)
    if hit then
        if hit.Instance:IsDescendantOf(target.Parent) then
            return true     
        end
    end
    return false
end

function updateTarget()
    local dist = settingsFolder.Range.Value
    local tempTarget
    for i,model in pairs(workspace:GetChildren()) do
        local enemyHuman = model:FindFirstChildWhichIsA("Humanoid")
        if model ~= script.Parent and enemyHuman and enemyHuman.Health>0 and enemyHuman.RootPart then

            local enemySettings = model:FindFirstChild("Settings")
            local enemyTeam
            local team = false
            if enemySettings then
                enemyTeam = enemyTeam:FindFirstChild("TEAM")
            else
                enemyTeam = model:FindFirstChild("TEAM")
            end
            if enemyTeam then
                if enemyTeam.Value == settingsFolder.TEAM.Value then
                    team = true
                end
            end

            if not settingsFolder.AttackPlayers.Value then
                if game.Players:GetPlayerFromCharacter(model) then
                    team = true
                end
            end

            local enemyTorso = enemyHuman.RootPart
            local tempDist = checkDist(enemyTorso)
            if not team and tempDist < dist and tempDist > 8 and checkSight(enemyTorso) then
                tempTarget = enemyTorso
                dist = tempDist
            end
        end
    end
    currentTarget = tempTarget
end

function aim(target)
    local dist = checkDist(target)
    local targetPos = target.Position + target.Velocity/(3.9 - dist/30)
    local newCFrame = CFrame.new(aimer.Position,targetPos)
    local lookDiff = (newCFrame.LookVector - aimer.CFrame.LookVector).Magnitude
    aimAttach.WorldCFrame = newCFrame
end

function shootTarget()
    if (barrel.RotVelocity).Magnitude > 10 then
        
        lightFlash.Enabled = true
        spawner(function() wait(0.1) lightFlash.Enabled = false end)
        smoke.Enabled = true
        
        currentFlash += 1 
        if currentFlash > 3 then currentFlash = 1 end
        particleFlash[currentFlash]:Emit(1)
        
        local case = Instance.new("Part")
        case.Size = Vector3.new(0.2,0.2,0.7)
        case.Material = Enum.Material.Metal
        case.CanCollide = false
        case.BrickColor = BrickColor.new("New Yeller")
        case.CFrame = body.Eject.WorldCFrame * CFrame.Angles(0,math.rad(180),0)
        case.Velocity = case.CFrame.RightVector * -10
        case.Parent = workspace
        spawner(function() wait(0.2) case.CanCollide = true end)
        game:GetService("Debris"):AddItem(case,2)
        
        local bullet = Instance.new("Part")
        bullet.Size = Vector3.new(0.1,0.1,0.5)
        bullet.Material = Enum.Material.Neon
        bullet.CanCollide = false
        bullet.BrickColor = BrickColor.new("New Yeller")
        bullet.CFrame = barrelChamber.WorldCFrame
        bullet.Anchored = true
        bullet.Parent = script.Parent
        
        local fileMesh = Instance.new("FileMesh")
        fileMesh.MeshId = "rbxassetid://5435476321"
        fileMesh.TextureId = "rbxassetid://5435476346"
        fileMesh.Scale = Vector3.new(0.2,0.2,0.15)
        fileMesh.Parent = case
        
        local speed = 200--75
        local params = RaycastParams.new()
        local iterations = 50
        params.FilterDescendantsInstances = {script.Parent}
        local bulletConnection
        bulletConnection = game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
            local position,nextPosition = bullet.Position,bullet.CFrame.LookVector * deltaTime * speed
            local result = workspace:Raycast(position,nextPosition,params)
            if result then  
                bulletConnection:Disconnect()
                bullet:Destroy()
                local human = result.Instance.Parent:FindFirstChildWhichIsA("Humanoid")
                impact.WorldPosition = result.Position
                if human and human.Health > 0 then
                    human:TakeDamage(math.random(5,15))
                    impact.Hit:Play()
                    if human.Health <= 0 then
                        updateTarget()
                    end
                elseif not human then
                    impact.Miss:Play()
                    impact.Debris:Emit(1)
                    for i = 1, math.random(5,10) do
                        local chunk = Instance.new("Part")
                        chunk.Color = result.Instance.Color
                        chunk.Material = result.Instance.Material
                        local size = math.random(5)/10
                        chunk.Size = Vector3.new(size,size,size)
                        chunk.CFrame = CFrame.new(result.Position)
                        chunk.Velocity = Vector3.new(math.random(-20,20),math.random(10,20),math.random(-20,20))
                        chunk.Parent = workspace
                        game:GetService("Debris"):AddItem(chunk,1)
                    end
                end
            else
                bullet.Position = position + nextPosition
            end
            
            iterations -= 1
            if iterations < 0 then
                bulletConnection:Disconnect()
                bullet:Destroy()
            end
        end)
        
        local a0 = Instance.new("Attachment")
        a0.Position = Vector3.new(0.05,0,0.05)
        a0.Parent = bullet
        
        local a1 = Instance.new("Attachment")
        a1.Position = Vector3.new(-0.05,0,-0.05)
        a1.Parent = bullet
        
        local trail = Instance.new("Trail")
        trail.Color = ColorSequence.new(bullet.Color)
        trail.WidthScale = NumberSequence.new(1,0)
        trail.Lifetime = 0.2
        trail.Attachment0 = a0
        trail.Attachment1 = a1
        trail.Parent = bullet
        
        local shootSfx = shootSound:Clone()
        shootSfx.Parent = body
        shootSfx:Play()
        game:GetService("Debris"):AddItem(shootSfx,1)
        
        game:GetService("Debris"):AddItem(bullet,10)
    end
end

function combatLoop()
    while human.Health > 0 do
        if currentTarget and canSeeTarget then
            tween:Create(barrelHinge,TweenInfo.new(0.5),{AngularVelocity = 20}):Play()
            local lookDiff = (CFrame.new(body.Position,currentTarget.Position).LookVector - body.CFrame.LookVector).Magnitude
            if lookDiff < 0.5 then
                shootTarget()
            end
        else
            smoke.Enabled = false 
            tween:Create(barrelHinge,TweenInfo.new(2),{AngularVelocity = 0}):Play()
            wait(1)
        end
        wait(settingsFolder.FireRate.Value)
    end
    smoke.Enabled = false 
    tween:Create(barrelHinge,TweenInfo.new(2),{AngularVelocity = 0}):Play()
end

local noTargetIndex = 0
function aimLoop()
    while human.Health > 0 do
        if currentTarget then
            if checkSight(currentTarget) then
                noTargetIndex = 0
                canSeeTarget = true
                barrelHinge.AngularVelocity = barrelHinge.AngularVelocity
                aim(currentTarget)
            else
                canSeeTarget = false
            end
        else
            noTargetIndex += 1
            if noTargetIndex > 2 then
                noTargetIndex = 0
                local pos = body.Position + Vector3.new(math.random(-20,20),math.random(-2,3),math.random(-20,20))
                aim({Position = pos, Velocity = Vector3.new(0,0,0)})
            end
            wait(1)
        end
        wait()
    end
end

function updateTargetLoop()
    while human.Health > 0 do
        updateTarget()
        wait(3)
    end
end


spawner(aimLoop)
spawner(updateTargetLoop)
spawner(combatLoop)

human.Died:Connect(function()
    sparks.Enabled = true
    body.Shutdown:Play()
    aim({Position = (barrel.Position + barrel.CFrame.LookVector * 10) - Vector3.new(0,10,0),Velocity = Vector3.new(0,0,0)})
    wait(settingsFolder.RespawnDelay.Value)
    for i,v in pairs(script.Parent:GetChildren()) do
        if v:IsA("BasePart") then
            tween:Create(v,TweenInfo.new(0.5),{Transparency = 1}):Play()
            sparks.Enabled = false
            workspace["Turret"]:Destroy()
        end
    end
-   wait(0.5)
    if settingsFolder.Respawn.Value then
        clone.Parent = script.Parent.Parent
    end
    sparks.Enabled = false
    script.Parent:Destroy()
end)

这是抛出错误的代码。

enemyTeam = enemyTeam:FindFirstChild("TEAM")

我还应该提到这使得炮塔不会攻击其他任何人。

我尝试更改团队颜色,但什么也没做。

lua roblox
1个回答
0
投票

本地敌方队伍 将其设置为 nil 然后当你这样做时 敌人团队 = 敌人团队:FindFirstChild("TEAM") 它是一样的 nil:FindFirstChild("TEAM") nil 是当你不这样做时它设置的值 = (value)

© www.soinside.com 2019 - 2024. All rights reserved.