So I was following a tutorial but I am stuck because I got an error but I dont know how to fix it maybe you know whats wrong
my error:
my script:
local PS = game:GetService("PhysicsService")
local SS = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local SpawnTowerEvent = events:WaitForChild("SpawnTower")
local AnimateTowerEvent = events:WaitForChild("AnimateTower")
local tower = {}
function FindNearestTarget(newTower, range)
local nearestTarget = nil
for i, target in ipairs(workspace.Enemys:GetChildren()) do
local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
if distance < range then
nearestTarget = target
maxDistance = distance
end
end
return nearestTarget
end
function tower.Attack(newTower)
local config = newTower.Config
local target = FindNearestTarget(newTower, config.Range.Value)
if target and target:WaitForChild("Humanoid") and target.Humanoid.Health > 0 then
local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
newTower:WaitForChild("HumanoidRootPart").CFrame = targetCFrame
AnimateTowerEvent:FireAllClients(newTower, "Attack")
target:WaitForChild("Humanoid"):TakeDamage(config.Damage.Value)
task.wait(config.Cooldown.Value)
end
task.wait(0.1)
tower.Attack(newTower)
end
function tower.Spawn(player, name, cframe)
local towerExists = ReplicatedStorage.Towers:FindFirstChild(name)
if towerExists then
local newTower = towerExists:Clone()
newTower.HumanoidRootPart.CFrame = cframe
newTower.Parent = workspace.Towers
newTower.HumanoidRootPart:SetNetworkOwner(nil)
local CFrame = Instance.new("CFrameValue")
CFrame = Vector3.new(math.huge, math.huge, math.huge)
CFrame = newTower.HumanoidRootPart.CFrame
for i, object in ipairs(newTower:GetDescendants()) do
if object:IsA("BasePart") then
PS:SetPartCollisionGroup(object, "Tower")
end
end
coroutine.wrap(tower.Attack)(newTower)
else
warn("tower doesnt exitst;", name)
end
end
SpawnTowerEvent.OnServerEvent:Connect(tower.Spawn)
return tower
idk whats wrong but on line 18 maxDistance is also marked red for some reason.