So I have this function I think it is the problem but I can not figure out what the issue is. Here is my code.
local RunService = game:GetService("RunService")
local enemies = {}
local movingtimes = {}
local towers = {}
local target = {
function(tower,towerdata)
local furthest
for enemy,enemydata in pairs(enemies) do
if (enemy.Position-tower.Position).Magnitude < towerdata[3] then
if not furthest or (enemydata[1]*enemydata[2]) > enemies[furthest][1]*enemies[furthest][2] then
furthest = enemy
end
end
end
return furthest
end,
function(tower,towerdata)
local furthest
for enemy,enemydata in pairs(enemies) do
if (enemy.Position-tower.Position).Magnitude < towerdata[3] then
if not furthest or enemydata[1] < enemies[furthest][1] then
furthest = enemy
end
end
end
return furthest
end
}
for i=2,#workspace.Move:GetChildren() do
local prev = workspace.Move[string.format("%i",i-1)]
local part = workspace.Move[string.format("%i",i)]
table.insert(movingtimes,(part.Position-prev.Position).Magnitude)
end
RunService.Heartbeat:Connect(function(delta)
for tower,towerdata in pairs(towers) do
while os.clock()-towerdata[5] >= towerdata[1] do
local furthest = target[towerdata[6]](tower,towerdata)
if furthest then
game.ReplicatedStorage.Value.Value = furthest
local __,y = CFrame.new(tower.CFrame.p,furthest.CFrame.p):ToEulerAnglesYXZ()
tower.CFrame = CFrame.new(tower.CFrame.p)*CFrame.Angles(0,y,0)
game:GetService("ReplicatedStorage").Remotes.Sound:FireAllClients(tower)
enemies[furthest][3] -= towerdata[2]
towerdata[5] += towerdata[1]
if enemies[furthest][3] <= 0 then
--enemies[furthest] = nil
--furthest:Destroy()
end
else
towerdata[5] = os.clock() - towerdata[1]
break
end
end
end
for enemy,enemydata in pairs(enemies) do
enemydata[1] += delta
local elapsed = enemydata[1]
local done = false
for i,v in ipairs(movingtimes) do
if elapsed < v/enemydata[2] then
local CF1 = workspace.Move[string.format("%i",i)].CFrame
local CF2 = workspace.Move[string.format("%i",i+1)].CFrame
local P1 = CF1.Position
local P2 = CF2.Position
local LerpPos = P1:Lerp(P2,elapsed*enemydata[2]/v)
enemy.CFrame = CFrame.new(LerpPos,P2)
done = true
break
else
elapsed -= v/enemydata[2]
end
end
if not done then
enemy:Destroy()
enemies[enemy] = nil
end
end
end)
local function addtower(t,owner,pos,fire,dmg,range,thing,state)
local p = Instance.new("Part")
p.Anchored = true
p.Transparency = 1
p.CanCollide = false
p.Position = pos
p.Name = t.Name
p.Parent = workspace.Towers
towers[p] = {fire,dmg,range,thing,os.clock(),state}
end
addtower(game.ReplicatedStorage.Towers.Dummy,"QuiIity",workspace.thing.Position,.3,5,10,false,1)
local function move(e,speed,hp)
local p = Instance.new("Part")
p.Anchored = true
p.Transparency = 1
p.CanCollide = false
p.Name = e.Name
p.Parent = workspace.Enemy
enemies[p] = {0,speed,hp}
return enemies[p],p
end
The issue is most likely the first function in the target table also my issue is that the tower faces backwards and it plays the shoot sound.