Hello devs. I am having issues with a script I wrote to choose the closest Tower or NPC. It chooses the closest tower to :MoveTo(), but it will switch upon arrival. I need help figuring out what the issue is and how to solve it. Another issue I am having is during the repeat until loop. It is supposed to take away 75 health every 2 seconds but instead, it deals 150 damage every 2 seconds.
Here is my code (server script):
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MoveEvent = ReplicatedStorage:WaitForChild("MoveEvent")
function MoveFunction(player, NPC)
wait(1)
NPC.Humanoid.WalkSpeed = 6
NPC.Humanoid.JumpPower = 0
local TowerTable
if NPC:GetAttribute("Red") == true then
TowerTable = {game.Workspace.Tower1Blue, game.Workspace.Tower2Blue, game.Workspace.Tower3Blue}
else
TowerTable = {game.Workspace.Tower1Red, game.Workspace.Tower2Red, game.Workspace.Tower3Red}
end
local CurrSmallest = 100
local CurrMag = 2000
local Index
for i, tower in pairs(TowerTable) do
local mag = (tower.PrimaryPart.Position - NPC.PrimaryPart.Position).Magnitude
if mag < CurrMag then
CurrSmallest = tower
CurrMag = mag
Index = i
end
end
local npcTab = game.Workspace.NPCParts:GetChildren()
for i, npc in pairs(npcTab) do
if npc:GetAttribute("Red") == true and NPC:GetAttribute("Red") == false then
local mag = (npc.PrimaryPart.Position - NPC.PrimaryPart.Position).Magnitude
if mag < CurrMag then
CurrSmallest = npc
CurrMag = mag
Index = i
end
elseif npc:GetAttribute("Red") == false and NPC:GetAttribute("Red") == true then
local mag = (npc.PrimaryPart.Position - NPC.PrimaryPart.Position).Magnitude
if mag < CurrMag then
CurrSmallest = npc
CurrMag = mag
Index = i
end
end
end
print(TowerTable[Index])
if TowerTable[Index] == CurrSmallest then
NPC.Humanoid:MoveTo(TowerTable[Index].ExternalPrimPart.Position)
NPC.Humanoid.MoveToFinished:Connect(function()
if (NPC.PrimaryPart.Position - TowerTable[Index].PrimaryPart.Position).Magnitude > 4 then
NPC.Humanoid:MoveTo(TowerTable[Index].PrimaryPart.Position)
end
repeat
wait(1.5)
TowerTable[Index]:SetAttribute("Health", TowerTable[Index]:GetAttribute("Health") - 75)
until TowerTable[Index]:GetAttribute("Health") <= 0 or (NPC.PrimaryPart.Position - TowerTable[Index].PrimaryPart.Position).Magnitude > 4
end)
print('done hitting tower')
if TowerTable[Index]:GetAttribute("Health") < 1 then
print('Tower is dead')
TowerTable[Index].Parent = game.ServerStorage
local DestPart = game.ServerStorage.DestroyedPart:Clone()
DestPart.Parent = game.Workspace
DestPart.Position = Vector3.new(6.87, 16.5, -2.07)
DestPart.Size = Vector3.new(TowerTable[Index].MainPart.Size.X, 0.5, TowerTable[Index].MainPart.Size.Z)
DestPart.CanCollide = false
elseif NPC:GetAttribute("Health") < 1 then
NPC.PrimaryPart.Position = Vector3.new(0, 0, 0)
end
else
print(CurrSmallest)
NPC.Humanoid:MoveTo(CurrSmallest.PrimaryPart.Position)
if CurrSmallest:GetAttribute("Health") < 1 then
CurrSmallest.Parent = game.ServerStorage
script.Parent.Parent = game.ServerStorage
local DestPart = game.ServerStorage.DestroyedPart:Clone()
DestPart.Parent = game.Workspace
DestPart.Position = Vector3.new(6.87, 16.5, -2.07)
DestPart.Size = Vector3.new(CurrSmallest.MainPart.Size.X, 0.5, CurrSmallest.MainPart.Size.Z)
DestPart.CanCollide = false
end
end
end
MoveEvent.OnServerEvent:Connect(MoveFunction)
Also, I will add pathfinding instead of MoveTo, but I wanted to figure out basic functions first.