NPC Doesn't Attack a Specific TeamColor?
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:
01 | local larm = script.Parent:FindFirstChild( "Left Arm" ) |
02 | local rarm = script.Parent:FindFirstChild( "Right Arm" ) |
06 | local wanderConeSize = 6 |
07 | local maxWanderAngle = 0.75 |
09 | function getWanderTarget() |
11 | wanderAngle = (math.random() - 0.5 ) * maxWanderAngle |
12 | rotatedLookVector = CFrame.Angles( 0 , wanderAngle, 0 ) * script.Parent.Torso.CFrame.lookVector |
13 | return (script.Parent.Torso.Position + wanderConeSize * rotatedLookVector) |
17 | local missile = script.Parent.Rocket:clone() |
18 | missile.CFrame = spn.CFrame * CFrame.new( 0 , 0 , - 6 ) |
19 | local creator_tag = Instance.new( "ObjectValue" ) |
20 | creator_tag.Value = game.Players.LocalPlayer |
21 | creator_tag.Name = "creator" |
22 | creator_tag.Parent = missile |
23 | missile.Kill.Disabled = false |
24 | missile.Transparency = 0 |
25 | missile.Light.Light.Visible = true |
26 | missile.RocketScript.Disabled = false |
27 | script.Parent.Rocket.PewPew:play() |
28 | missile.Parent = game.Workspace |
29 | script.Parent.Flash.Light.Light.Visible = true |
31 | script.Parent.Flash.Light.Light.Visible = false |
34 | function findNearestTorso(pos) |
35 | local list = game.Workspace:children() |
43 | if (temp 2. className = = "Model" ) and (temp 2 ~ = script.Parent) then |
44 | temp = temp 2 :findFirstChild( "Right Arm" ) |
45 | human = temp 2 :findFirstChild( "Humanoid" ) |
46 | if (temp ~ = nil ) and (human ~ = nil ) and (human.Health > 0 ) then |
47 | if (temp.Position - pos).magnitude < dist then |
49 | dist = (temp.Position - pos).magnitude |
57 | human = script.Parent:findFirstChild( "Humanoid" ) |
58 | if human = = nil then human = script.Parent:findFirstChild( "Zombie" ) end |
61 | wait(math.random( 0.5 , 1 )) |
62 | local target = findNearestTorso(script.Parent.Torso.Position) |
64 | fire(script.Parent.Torso) |
65 | human:MoveTo(target.Position, target) |
66 | human.TargetPoint = target.Position |
68 | wanderTarget = getWanderTarget() |
69 | human:MoveTo(wanderTarget, script.Parent.Torso) |
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:
01 | local larm = script.Parent:FindFirstChild( "Left Arm" ) |
02 | local rarm = script.Parent:FindFirstChild( "Right Arm" ) |
06 | local wanderConeSize = 6 |
07 | local maxWanderAngle = 0.75 |
09 | function getWanderTarget() |
11 | wanderAngle = (math.random() - 0.5 ) * maxWanderAngle |
12 | rotatedLookVector = CFrame.Angles( 0 , wanderAngle, 0 ) * script.Parent.Torso.CFrame.lookVector |
13 | return (script.Parent.Torso.Position + wanderConeSize * rotatedLookVector) |
17 | local missile = script.Parent.Rocket:clone() |
18 | missile.CFrame = spn.CFrame * CFrame.new( 0 , 0 , - 6 ) |
19 | local creator_tag = Instance.new( "ObjectValue" ) |
20 | creator_tag.Value = game.Players.LocalPlayer |
21 | creator_tag.Name = "creator" |
22 | creator_tag.Parent = missile |
23 | missile.Kill.Disabled = false |
24 | missile.Transparency = 0 |
25 | missile.Light.Light.Visible = true |
26 | missile.RocketScript.Disabled = false |
27 | script.Parent.Rocket.PewPew:play() |
28 | missile.Parent = game.Workspace |
29 | script.Parent.Flash.Light.Light.Visible = true |
31 | script.Parent.Flash.Light.Light.Visible = false |
34 | function findNearestTorso(pos) |
35 | local list = game.Workspace:children() |
43 | if (temp 2. className = = "Model" ) and (temp 2 ~ = script.Parent) then |
44 | temp = temp 2 :findFirstChild( "Right Arm" ) |
45 | human = temp 2 :findFirstChild( "Humanoid" ) |
46 | if game.Players:GetPlayerFromCharacter(human.Parent).TeamColor = = "Maroon" then |
47 | if (temp ~ = nil ) and (human ~ = nil ) and (human.Health > 0 ) then |
48 | if (temp.Position - pos).magnitude < dist then |
50 | dist = (temp.Position - pos).magnitude |
59 | human = script.Parent:findFirstChild( "Humanoid" ) |
60 | if human = = nil then human = script.Parent:findFirstChild( "Zombie" ) end |
63 | wait(math.random( 0.5 , 1 )) |
64 | local target = findNearestTorso(script.Parent.Torso.Position) |
65 | if game.Players:GetPlayerFromCharacter(target.Parent).TeamColor = = "Maroon" then |
67 | fire(script.Parent.Torso) |
68 | human:MoveTo(target.Position, target) |
69 | human.TargetPoint = target.Position |
71 | wanderTarget = getWanderTarget() |
72 | human:MoveTo(wanderTarget, script.Parent.Torso) |
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