im making a unit system for my tower defense game anyone can help?
task.wait(1.5)
local RunService = game:GetService("RunService")
local PhysicsService = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local TweenService = game:GetService("TweenService")
local animateZombieEvent = game:GetService("ReplicatedStorage").Events:WaitForChild("AnimateZombie")
local animateTowerEvent = game:GetService("ReplicatedStorage").Events:WaitForChild("AnimateTower")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local animateUnitEvent = events:WaitForChild("AnimateTower")
local Unit = {}
local function setAnimation(object, animName)
local AnimationController = object:FindFirstChild("AnimationController")
if AnimationController then
local animationObject = AnimationController:FindFirstChild(animName)
if animationObject then
local playingTracks = AnimationController:GetPlayingAnimationTracks()
for i, track in pairs(playingTracks) do
if track.Name == animName then
return track
end
end
local animationTrack = AnimationController:LoadAnimation(animationObject)
return animationTrack
end
end
end
function Unit.Move(newUnit, UnitConfig, Path)
local Health = game["Workspace"]["Units"][newUnit][UnitConfig]:FindFirstChild("Health")
local Map = workspace.Map:FindFirstChildOfClass("Folder")
local config = game["Workspace"]["Units"][newUnit]:WaitForChild("Config", 0.5)
local EnemyRoot = game["Workspace"]["Units"][newUnit]:WaitForChild("HumanoidRootPart")
local EnemyPosition = EnemyRoot:WaitForChild("AlignPosition")
local EnemyOrientation = EnemyRoot:WaitForChild("AlignOrientation")
local NextPath = game["Workspace"]["Units"][newUnit]:FindFirstChild("MovingTo").Value
while wait(0.3) do
local function FindNearestTarget()
if config then
local mobs = workspace.Mobs
local maxDistance = config.Range.Value
local maxDistance2 = 1
local nearestTarget = nil
local TouchTarget = nil
for i, target in pairs(mobs:GetChildren()) do
local distance = (target.HumanoidRootPart.Position - newUnit.HumanoidRootPart.Position).Magnitude
if not target:FindFirstChild("Config") then
continue
end
if distance < maxDistance then
nearestTarget = target
maxDistance = distance
end
if distance < maxDistance2 then
TouchTarget = target
maxDistance2 = distance
end
end
task.wait()
return nearestTarget, TouchTarget
end
end
local Attack = setAnimation(newUnit, "Attack")
local target, target2 = FindNearestTarget(), FindNearestTarget()
print(target, target2)
if target == nil then
else
local UH = config.Health.Value
if target2 == nil then
else
newUnit:WaitForChild("Config", 0.5).Health.Value -= target2:WaitForChild("Config", 0.5).Health.Value
target2.Config:WaitForChild("Health", 0.5).Value -= newUnit:WaitForChild("Config", 0.5).Health.Value
end
if newUnit:FindFirstChild("Config") then
if target2 == nil then
else
newUnit:WaitForChild("Config", 0.5).Health.Value -= target2:WaitForChild("Config", 0.5).Health.Value
target2.Config:WaitForChild("Health", 0.5).Value -= newUnit:WaitForChild("Config", 0.5).Health.Value
end
EnemyRoot.Anchored = true
newUnit:WaitForChild("Config", 0.5).Speed.Value = 0
newUnit.Attack.Value = true
wait(newUnit:WaitForChild("Config", 0.5).Cooldown.Value)
Attack:Play()
print(Health)
Health.Value -= newUnit.Config.Damage.Value
end
EnemyRoot.Anchored = false
newUnit:WaitForChild("Config", 0.5).Speed.Value = 4
newUnit.Attack.Value = false
end
if Health and Health.Value <= 0 then
break
end
if (EnemyRoot.Point.WorldPosition - Path[NextPath].Point.WorldPosition).Magnitude < 1 then
if NextPath < #Path:GetChildren() then
NextPath = NextPath + 1
else
break
end
else
EnemyPosition.Attachment1 = Path[NextPath].Point
EnemyPosition.MaxVelocity = UnitConfig.Speed.Value
EnemyOrientation.Attachment1 = Path[NextPath].Point
end
if Health and Health.Value <= 0 then
break
end
task.wait()
end
if NextPath >= #Path:GetChildren() then
newUnit:Destroy()
end
end
function Unit.Spawn(name, quantity, Map, plr)
local UnitExists = ServerStorage.SpawnTroop:FindFirstChild(name)
local Map = workspace.Map:FindFirstChildOfClass("Folder")
if UnitExists then
for i=1, quantity do
task.wait(1)
if Map:FindFirstChild("UnitStart") then
local newUnit = UnitExists:Clone()
local range = newUnit:WaitForChild("Config", 0.5).Range.Value
local height = (newUnit.PrimaryPart.Size.Y/ 2)
local offset = CFrame.new(0, -height, 0)
local rngPart = Instance.new("Part")
rngPart.Size = Vector3.new(0.1, newUnit.Config.Range.Value, newUnit.Config.Range.Value)
rngPart.Name = "RangePart"
rngPart.Shape = Enum.PartType.Cylinder
rngPart.Material = Enum.Material.SmoothPlastic
rngPart.Transparency = 1
rngPart.Color = Color3.new(0.243137, 0.509804, 1)
rngPart.CFrame = CFrame.new(newUnit.PrimaryPart.CFrame.X, newUnit.PrimaryPart.CFrame.Y - 1, newUnit.PrimaryPart.CFrame.Z) * offset * CFrame.Angles(0, 0, math.rad(90))
rngPart.Parent = newUnit
local weld = Instance.new("WeldConstraint")
weld.Parent = newUnit.HumanoidRootPart
weld.Part1 = newUnit.HumanoidRootPart
weld.Part0 = rngPart
PhysicsService:SetPartCollisionGroup(rngPart, "Air")
local cframe = CFrame.new(Map.UnitStart.Position)
newUnit:SetPrimaryPartCFrame(cframe)
newUnit.Parent = workspace.Units
newUnit.Name = plr.DisplayName .. " ".. newUnit.Name
local UnitConfig = newUnit:FindFirstChild("Config")
local Path = Map.UnitWaypoints
local MovingTo = Instance.new("IntValue")
MovingTo.Name = "MovingTo"
MovingTo.Parent = newUnit
MovingTo.Value = 1
local UnitTag = Instance.new("BoolValue")
UnitTag.Name = "UnitTag"
UnitTag.Parent = newUnit
local Attack = Instance.new("BoolValue")
Attack.Name = "Attack"
Attack.Parent = newUnit
newUnit.PrimaryPart = newUnit.HumanoidRootPart
newUnit.HumanoidRootPart.Anchored = false
newUnit.HumanoidRootPart:SetNetworkOwner(nil)
newUnit.Config.Speed.Value += 2
for i, object in ipairs(newUnit:GetDescendants()) do
if object:IsA("BasePart") then
object.Anchored = false
wait(0.1)
object:SetNetworkOwner(nil)
PhysicsService:SetPartCollisionGroup(object, "Units")
end
end
coroutine.wrap(Unit.Move)(newUnit, UnitConfig, Path)
task.wait(0.1)
local Idle = setAnimation(newUnit, "Idle")
local Walk = setAnimation(newUnit, "Walk")
Walk:Play()
Attack.Changed:Connect(function(deb)
if deb == true then
Walk:Stop()
Idle:Play()
else
Walk:Play()
Idle:Stop()
end
end)
end
end
else
warn("Unit Not Exist: ", name)
end
workspace.Info.Wave.Changed:Connect(function()
Unit.Spawn("Tropper 2", 1, workspace.Map:FindFirstChildOfClass("Folder"), game.Players:FindFirstChildOfClass("Player"))
end)
end
return Unit
here is the error:
ServerScriptService.Script.Main.Unit:207: ServerScriptService.Script.Main.Unit:35: invalid argument #2 (string expected, got Instance)