I am currently working on a small project for fun, and I’ve been watching a lot of videos trying to figure it out. However, I’ve come across an issue I can’t seem to fix, it’s very small but super annoying.
In this game I have two factions of npcs. The “Hungry Bandits” and “Dust Bandits”. They are supposed to be hostile towards the player and towards each other but ignore their own members. I have their controller script set up so that they are hostile towards players, and they attack each other if I spawn them directly into the workspace. However, if I spawn them through a spawn block, they remain hostile to players but start to ignore each other! Can anyone help me figure out why?
this is the controller script I have set up:
local hum = script.Parent:WaitForChild("Humanoid")
local root = script.Parent:WaitForChild("HumanoidRootPart")
local spawnPos = script.Parent:WaitForChild("SpawnPosition")
local thisFaction = hum:WaitForChild("Faction")
local visionRange = 30
local attackRange = script.AttackRange.Value
local abandonRange = 50
local abandonRangeFromHome = 200
local target = nil
while wait() do
if target then
target.Humanoid.HealthChanged:Connect(function(newHealth)
if newHealth <= 0 then
target = nil
hum:MoveTo(spawnPos.Value)
end
end)
--calculate distance and attack player
local plrRoot = target.HumanoidRootPart
local distance = (root.Position - plrRoot.Position).magnitude
local distanceFromHome = (spawnPos.Value - plrRoot.Position).magnitude
hum:MoveTo(plrRoot.Position - CFrame.new(root.Position, plrRoot.Position).LookVector * attackRange)
if distance <= attackRange + 2 then
script.AttackRemote:Fire(plrRoot)
end
if distance > abandonRange then
target = nil
hum:MoveTo(spawnPos.Value)
end
if distanceFromHome > abandonRangeFromHome then
target = nil
hum:MoveTo(spawnPos.Value)
end
else
--see if any players are in range
for i, v in pairs(game.Workspace:GetChildren()) do
if not game.Workspace:FindFirstChild(v.Name) then continue end
local char = game.Workspace[v.Name]
local me = script.Parent
if char:FindFirstChild("HumanoidRootPart") and char ~= me then
local plrRoot = char:FindFirstChild("HumanoidRootPart")
local hum = char:FindFirstChild("Humanoid")
local faction = hum:FindFirstChild("Faction")
local distance = (root.Position - plrRoot.Position).magnitude
local distanceFromHome = (spawnPos.Value - plrRoot.Position).magnitude
if distance < visionRange and distanceFromHome < abandonRangeFromHome then
if faction == nil or faction.Value ~= thisFaction.Value then
if hum.Health > 0 then
target = char
end
end
end
end
end
end
end
Okay, I think I found one of the issues… The Dust Bandits only target 1 specific Hungry Bandit or Hungry Bandit Boss at a time. I think these are the first ones on the workspace list, I don’t know why they do that, but im pretty sure that is helpful in figuring out what the issue is here
these is my spawner scripts:
local HungryBandits = game.Workspace.SpawnHungryBandit:GetChildren()
local DustBandits = game.Workspace.SpawnDustBandit:GetChildren()
for i,v in pairs(HungryBandits) do
local respawn = script.HungrySpawner:Clone()
respawn.Parent = v
respawn.Disabled = false
end
for i,v in pairs(DustBandits) do
local respawn = script.DustSpawner:Clone()
respawn.Parent = v
respawn.Disabled = false
end
local serverStorage = game:GetService("ServerStorage")
local rng = math.random(1, 5)
if rng <= 4 then
enemy = serverStorage:WaitForChild("Hungry Bandit"):Clone()
else
enemy = serverStorage:WaitForChild("Hungry Bandit Boss"):Clone()
end
enemy.HumanoidRootPart.Position = script.Parent.Position
local spawnpos = enemy:WaitForChild("SpawnPosition")
spawnpos.Value = script.Parent.Position
local respawntime = 60
enemy.Parent = game.Workspace
enemy.Humanoid.HealthChanged:Connect(function(newHealth)
if newHealth <= 0 then
wait(respawntime)
script.Disabled = true
script.Disabled = false
end
end)
and the same but with Dust Bandits