hello everyone I’m making a tower defense game and the problem is that the Enemy Handler isn’t spawning a single enemy I searched everywhere but couldn’t find a solution I had a similar problem but this time I add new stuff and it happened again.
Note: in the Enemies folder there is supposed to be random names of the enemy for example asdSa18-adw1CJ-adacJ7
EnemyHandler
local EHandler = {List={}}
local HttpService = game:GetService("HttpService")
local TweenService = game:GetService("TweenService")
local ServerStorage = game.ServerStorage
local Map = game.Workspace["Tower Defense Map"]
local WalkPoints = Map.Movement
local Enemies = Map.Enemies
local PlayerBase = Map.Bases.PlayerBase
local function OnFinish(Enemy)
Enemy.Body:Destroy();
PlayerBase.Health.Value -= Enemy.Details.Damage
end
local MoveEnemy
MoveEnemy = function(Enemy)
if not(WalkPoints:FindFirstChild(Enemy.CurrentPoint)) then
return OnFinish()
end
local Info = TweenInfo.new(2);
TweenService:Create(Enemy.Body, TweenInfo.new(2,Enum.EasingStyle.Linear), {
Position = WalkPoints[tostring(Enemy.CurrentPoint)].Position
}):Play()
Enemy.CurrentPoint += 1
task.delay(2, MoveEnemy, Enemy)
end
EHandler.doDamage = function(EntityGUID, Damage)
local Entity = EHandler.List[EntityGUID]
if not Entity then return warn('GUID not found in Entity List') end
Entity.Health -= Damage
if Entity.Health <= 0 then
Entity.Body:Destroy()
end
end
EHandler.createEnemy = function(Details)
-- Will later be used to determine health, type, and etc...
local GUID = HttpService:GenerateGUID(false)
local Enemy = ServerStorage.Enemy:Clone()
Enemy.Parent = Enemies
Enemy.Position = WalkPoints["1"].Position
Enemy.Name = GUID
local EnemyDetails = {
Body = Enemy,
CurrentPoint = 1,
Details = Details,
OnDamage = function(Damage)end,
}
--Future..
EHandler.List[GUID] = EnemyDetails
MoveEnemy(EnemyDetails)
return EnemyDetails
end
return EHandler
EntitySpawner
local EnemyHandler = require(script.EnemyHandler)
local playerUnitHandler = require(script.PlayerUnitHandler)
local Map = game.Workspace["Tower Defense Map"]
local Bases = Map.Bases
local EnemyBase = game.Workspace["Tower Defense Map"].Bases.EnemyBase
local PlayerBase = game.Workspace["Tower Defense Map"].Bases.PlayerBase
local Enemies = Map.Enemies
playerUnitHandler.createUnit({
Damage = 1,
Range = 15,
AttackDelay = 2
})
for i = 1, 10 do
EnemyHandler.createEnemy({
Health = 1,
})
task.wait(2)
end
sorry for the late response I was taking a break but here is the PlayerUnitHandler
PlayerUnitHandler
local PUnitHeandler = {List={}}
local ServerStorage = game.ServerStorage
local Map = game.Workspace["Tower Defense Map"]
local Units = Map.Units
local Enemies = Map.Enemies
local function getClosetEnemy(Range)
local ClosestEnemy, Distance = nil, math.huge
for _, Enemy in next, Enemies:GetChildren() do
local Distance = (Unit.Position - Enemy.Position).Magnitude
if Distance <= Distance then
ClosestEnemy = Enemy
ClosestDistance = Distance
end
end
return ClosestEnemy, Distance
end
PUnitHeandler.createUnit = function(Details)
local Unit = ServerStorage.Unit:Clone()
Unit.Parent = Units
Unit.Radius.Size = Vector3.new(Details.Range, Details.Range, Details.Range)
Unit.Radius.Position = Unit.Position
Unit.Radius.Transparency = 1
while task.wait(Details.AttackDelay) do
local Enemy = getClosetEnemy(Unit, Details.Range)
if not Enemy then continue end
print(Enemy)
end
end
return PUnitHeandler
Sorry for the late response I was frustrated yesterday and today I just came from school pardon me in advance
local PUnitHeandler = {List={}}
local ServerStorage = game.ServerStorage
local Map = game.Workspace["Tower Defense Map"]
local Units = Map.Units
local Enemies = Map.Enemies
local function getClosetEnemy(Range)
local ClosestEnemy, Distance = nil, math.huge
for _, Enemy in next, Enemies:GetChildren() do
local Distance = (Unit.Position - Enemy.Position).Magnitude
if Distance <= Distance then
ClosestEnemy = Enemy
ClosestDistance = Distance
end
end
return ClosestEnemy, Distance
end
PUnitHeandler.createUnit = function(Details)
local Unit = ServerStorage.Unit:Clone()
Unit.Parent = Units
Unit.Radius.Size = Vector3.new(Details.Range, Details.Range, Details.Range)
Unit.Radius.Position = Unit.Position
Unit.Radius.Transparency = 1
while task.spawn(Details.AttackDelay) do
local Enemy = getClosetEnemy(Unit, Details.Range)
if not Enemy then continue end
print(Enemy)
end
end
return PUnitHeandler
task.spawn(function()
while Enemy:IsDescendantOf(workspace) do
local Enemy = getClosetEnemy(Unit, Details.Range)
if not Enemy then continue end
print(Enemy)
task.wait()
end
end)
Here I run this in a nice spawn so it doesn’t yield the script. The flaw with your while loop too is that, if the enemy was destroyed, task.wait() will always return a truthy value no matter what so your while loop would run forever thus building up memory
task.spawn(function()
while Unit:IsDescendantOf(workspace) do
local Enemy = getClosetEnemy(Unit, Details.Range)
if not Enemy then continue end
print(Enemy)
task.wait()
end
end)
15:01:25.016 ServerScriptService.EntitySpawner.PlayerUnitHandler:38: Expected 'end' (to close 'function' at line 22), got <eof>; did you forget to close 'do' at line 30? - Studio - PlayerUnitHandler:38
15:01:25.016 Requested module experienced an error while loading - Server - EntitySpawner:2
15:01:25.017 Script 'ServerScriptService.EntitySpawner', Line 2 - Studio - EntitySpawner:2
PlayerUnitHandler
15:01:17.558 Baseplate.rbxl auto-recovery file was created - Studio
15:01:25.016 ServerScriptService.EntitySpawner.PlayerUnitHandler:38: Expected 'end' (to close 'function' at line 22), got <eof>; did you forget to close 'do' at line 30? - Studio - PlayerUnitHandler:38
enemy handler is the script controlling the enemies but it has no errors in the output
ServerScriptService.EntitySpawner.PlayerUnitHandler:38: Expected ‘end’ (to close ‘function’ at line 22), got ; did you forget to close ‘do’ at line 30?
task.spawn(function()
while Unit:IsDescendantOf(workspace) do
local Enemy = getClosetEnemy(Unit, Details.Range)
if not Enemy then continue end
print(Enemy)
task.wait()
end
end)
end
Output
16:26:07.321 Baseplate.rbxl auto-recovery file was created - Studio
16:26:15.015 cloud_340936329.GUIEditor.PluginTool:4: attempt to call a nil value - Server
16:26:15.015 Stack Begin - Studio
16:26:15.015 Script 'cloud_340936329.GUIEditor.PluginTool', Line 4 - Studio
16:26:15.015 Stack End - Studio
16:26:15.015 Requested module experienced an error while loading - Server
16:26:15.015 Stack Begin - Studio
16:26:15.016 Script 'cloud_340936329.GUIEditor.Plugin', Line 20 - Studio
16:26:15.016 Stack End - Studio
16:26:15.725 Workspace.Script:6: Expected identifier when parsing expression, got ')' - Studio - Script:6
16:26:15.740 Combat is not a valid member of ReplicatedStorage "ReplicatedStorage" - Server - M2:1
16:26:15.741 Stack Begin - Studio
16:26:15.741 Script 'Workspace.Model.Nichirins.Katana.Combat.M2', Line 1 - Studio - M2:1
16:26:15.741 Stack End - Studio
16:26:15.747 Unit is not a valid member of ServerStorage "ServerStorage" - Server - PlayerUnitHandler:23
16:26:15.747 Stack Begin - Studio
16:26:15.748 Script 'ServerScriptService.EntitySpawner.PlayerUnitHandler', Line 23 - Studio - PlayerUnitHandler:23
16:26:15.748 Script 'ServerScriptService.EntitySpawner', Line 12 - Studio - EntitySpawner:12
16:26:15.748 Stack End - Studio
16:26:15.749 Combat is not a valid member of ReplicatedStorage "ReplicatedStorage" - Server - Script:2
16:26:15.749 Stack Begin - Studio
16:26:15.749 Script 'Workspace.Model.Nichirins.Katana.Combat.RemoteEvent.Script', Line 2 - Studio - Script:2
16:26:15.750 Stack End - Studio
16:26:15.750 Combat is not a valid member of ReplicatedStorage "ReplicatedStorage" - Server - Script:2
16:26:15.751 Stack Begin - Studio
16:26:15.751 Script 'Workspace.Model.Nichirins.Katana.Combat.RemoteEvent.Script', Line 2 - Studio - Script:2
16:26:15.751 Stack End - Studio
16:26:15.751 Combat is not a valid member of ReplicatedStorage "ReplicatedStorage" - Server - M2:1
16:26:15.751 Stack Begin - Studio
16:26:15.751 Script 'Workspace.Model.WaterSlayer.Katana.Combat.M2', Line 1 - Studio - M2:1
16:26:15.751 Stack End - Studio
16:26:15.752 Workspace.Kizuki Pose Set.READ ME:1: Incomplete statement: expected assignment or a function call - Studio - READ ME:1
16:26:15.752 StandAppearParticles is not a valid member of ReplicatedStorage "ReplicatedStorage" - Server - StandAppearParticles:1
16:26:15.752 Stack Begin - Studio
16:26:15.752 Script 'Workspace.Stand summon particle & summon animation.StandAppearParticles', Line 1 - Studio - StandAppearParticles:1
16:26:15.753 Stack End - Studio
16:26:15.753 Workspace.Model.Urokodaki.ClickPart.QuestTake:8: Expected 'end' (to close 'function' at line 1), got <eof>; did you forget to close 'then' at line 3? - Studio - QuestTake:8
16:26:15.756 Combat is not a valid member of ReplicatedStorage "ReplicatedStorage" - Server - Script:2
16:26:15.756 Stack Begin - Studio
16:26:15.756 Script 'Workspace.Model.WaterSlayer.Katana.Combat.RemoteEvent.Script', Line 2 - Studio - Script:2
16:26:15.756 Stack End - Studio
16:26:15.758 Combat is not a valid member of ReplicatedStorage "ReplicatedStorage" - Server - M2:1
16:26:15.758 Stack Begin - Studio
16:26:15.758 Script 'Workspace.Model.Nichirins.Katana.Combat.M2', Line 1 - Studio - M2:1
16:26:15.758 Stack End - Studio
16:26:15.762 Combat is not a valid member of ReplicatedStorage "ReplicatedStorage" - Server - Script:2
16:26:15.762 Stack Begin - Studio
16:26:15.762 Script 'Workspace.Model.Nichirins.Katana.Combat.RemoteEvent.Script', Line 2 - Studio - Script:2
16:26:15.762 Stack End - Studio
16:26:15.763 Combat is not a valid member of ReplicatedStorage "ReplicatedStorage" - Server - M2:1
16:26:15.763 Stack Begin - Studio
16:26:15.763 Script 'Workspace.Model.Nichirins.Katana.Combat.M2', Line 1 - Studio - M2:1
16:26:15.763 Stack End - Studio
16:26:15.766 Combat is not a valid member of ReplicatedStorage "ReplicatedStorage" - Server - M2:1
16:26:15.766 Stack Begin - Studio
16:26:15.766 Script 'Workspace.Model.Nichirins.Katana.Combat.M2', Line 1 - Studio - M2:1
16:26:15.767 Stack End - Studio
16:26:15.785 Combat is not a valid member of ReplicatedStorage "ReplicatedStorage" - Server - Script:2
16:26:15.785 Stack Begin - Studio
16:26:15.786 Script 'Workspace.Model.Nichirins.Katana.Combat.RemoteEvent.Script', Line 2 - Studio - Script:2
16:26:15.786 Stack End - Studio
16:26:20.250 Ro-Defender has removed a total of 0 viruses and junk items from your games! - Server
16:26:22.624 cloud_340936329.GUIEditor.PluginTool:4: attempt to call a nil value - Client
16:26:22.626 Stack Begin - Studio
16:26:22.626 Script 'cloud_340936329.GUIEditor.PluginTool', Line 4 - Studio
16:26:22.626 Stack End - Studio
16:26:22.626 Requested module experienced an error while loading - Client
16:26:22.627 Stack Begin - Studio
16:26:22.627 Script 'cloud_340936329.GUIEditor.Plugin', Line 20 - Studio
16:26:22.627 Stack End - Studio
16:26:34.246 Ro-Defender has removed a total of 0 viruses and junk items from your games! - Client
16:26:48.331 HumanoidRootPart is not a valid member of Model "Workspace.Model.WaterSlayer" - Server - FollowAndAttack:59
16:26:48.332 Stack Begin - Studio
16:26:48.332 Script 'Workspace.Model.WaterSlayer.FollowAndAttack', Line 59 - function findTargets - Studio - FollowAndAttack:59
16:26:48.332 Script 'Workspace.Model.WaterSlayer.FollowAndAttack', Line 196 - function onHeartbeat - Studio - FollowAndAttack:196
16:26:48.333 Script 'Workspace.Model.WaterSlayer.FollowAndAttack', Line 203 - Studio - FollowAndAttack:203
16:26:48.333 Stack End - Studio
16:26:48.437 Torso is not a valid member of Model "Workspace.Model.WaterSlayer" - Server - Water1:51
16:26:48.437 Stack Begin - Studio
16:26:48.437 Script 'Workspace.Model.WaterSlayer.Water1', Line 51 - Studio - Water1:51
16:26:48.437 Stack End - Studio
16:26:49.361 Hair2 is not a valid member of Model "Workspace.Model.WaterSlayer" - Server - Respawn:6
16:26:49.362 Stack Begin - Studio
16:26:49.362 Script 'Workspace.Model.WaterSlayer.Respawn', Line 6 - Studio - Respawn:6
16:26:49.362 Stack End - Studio
I did this and I still didn’t work I’m sorry if I’m annoying you btw