Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

NPC Doesn't Attack a Specific TeamColor?

Asked by 7 years ago

Hello Everybody! My name is IronSpider671 and I am once again back here at ScriptingHelpers, because I seem to have utterly failed. So, I'm making a little game as a side project with NPCs and players fighting each other between two teams (not sure about the names yet but the colors are: Navy blue and Maroon). So, I wrote this little script, bits and pieces taken from tutorials, open source versions of games, and my own Lua knowledge. So, this script works, for the time being. I mean the NPCs run around and shoot every single Humanoid near them. This is my script:

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

local wanderTarget 
local wanderAngle 
local wanderConeSize = 6
local maxWanderAngle = 0.75 

function getWanderTarget()
    math.random(tick())
    wanderAngle = (math.random() - 0.5) * maxWanderAngle
    rotatedLookVector = CFrame.Angles(0, wanderAngle, 0) * script.Parent.Torso.CFrame.lookVector
    return (script.Parent.Torso.Position + wanderConeSize * rotatedLookVector)
end

function fire(spn)
    local missile = script.Parent.Rocket:clone()
    missile.CFrame = spn.CFrame * CFrame.new(0, 0, -6)
    local creator_tag = Instance.new("ObjectValue")
    creator_tag.Value = game.Players.LocalPlayer
    creator_tag.Name = "creator"
    creator_tag.Parent = missile
    missile.Kill.Disabled = false
    missile.Transparency = 0
    missile.Light.Light.Visible = true
    missile.RocketScript.Disabled = false
    script.Parent.Rocket.PewPew:play()
    missile.Parent = game.Workspace
    script.Parent.Flash.Light.Light.Visible = true
    wait(0.1)
    script.Parent.Flash.Light.Light.Visible = false
end

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 200
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("Right Arm")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end

human = script.Parent:findFirstChild("Humanoid")
if human == nil then human = script.Parent:findFirstChild("Zombie") end

while true do
    wait(math.random(0.5,1))
    local target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil then
    fire(script.Parent.Torso)
        human:MoveTo(target.Position, target)
        human.TargetPoint = target.Position
else
    wanderTarget = getWanderTarget()    
    human:MoveTo(wanderTarget, script.Parent.Torso)
    end

end

But, since my game is team-based, I wanted the NPCs to specifically attack the opposing team (or a specific TeamColor) So, I edited around and tried the NPC to just fire at and move to the torsos of players with the TeamColor, Maroon. This was my script afterwards:

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

local wanderTarget 
local wanderAngle 
local wanderConeSize = 6
local maxWanderAngle = 0.75 

function getWanderTarget()
    math.random(tick())
    wanderAngle = (math.random() - 0.5) * maxWanderAngle
    rotatedLookVector = CFrame.Angles(0, wanderAngle, 0) * script.Parent.Torso.CFrame.lookVector
    return (script.Parent.Torso.Position + wanderConeSize * rotatedLookVector)
end

function fire(spn)
    local missile = script.Parent.Rocket:clone()
    missile.CFrame = spn.CFrame * CFrame.new(0, 0, -6)
    local creator_tag = Instance.new("ObjectValue")
    creator_tag.Value = game.Players.LocalPlayer
    creator_tag.Name = "creator"
    creator_tag.Parent = missile
    missile.Kill.Disabled = false
    missile.Transparency = 0
    missile.Light.Light.Visible = true
    missile.RocketScript.Disabled = false
    script.Parent.Rocket.PewPew:play()
    missile.Parent = game.Workspace
    script.Parent.Flash.Light.Light.Visible = true
    wait(0.1)
    script.Parent.Flash.Light.Light.Visible = false
end

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 200
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("Right Arm")
            human = temp2:findFirstChild("Humanoid")
            if game.Players:GetPlayerFromCharacter(human.Parent).TeamColor == "Maroon" then
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end
end

human = script.Parent:findFirstChild("Humanoid")
if human == nil then human = script.Parent:findFirstChild("Zombie") end

while true do
    wait(math.random(0.5,1))
    local target = findNearestTorso(script.Parent.Torso.Position)
    if game.Players:GetPlayerFromCharacter(target.Parent).TeamColor == "Maroon" then
    if target ~= nil then
    fire(script.Parent.Torso)
        human:MoveTo(target.Position, target)
        human.TargetPoint = target.Position
else
    wanderTarget = getWanderTarget()    
    human:MoveTo(wanderTarget, script.Parent.Torso)
    end

end
end

After this, well... nothing. The NPC doesn't even move. It just stands there, not doing anything. I'm not sure what I did wrong (perhaps I put the if statement in a wrong location or so) but any help would be much appreciated! -IronSpider671

Answer this question