[Help] Tower Defense game Enemy Handler doesn't spawn enemies again

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.

screenshots


image
image

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
1 Like

Try debug with prints at first to see exactly where the code is stopping, also could you show the PlayerUnitHandler?

2 Likes

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

2 Likes

Here’s your issue, you’re running a while loop, this is when you use createUnit. Therefore the for loop below createUnit isn’t actually running.

Try running it in a task.spawn

1 Like


I fixed it but the enemies still didn’t spawn

1 Like

Could you show me your new code?

2 Likes

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
1 Like
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

2 Likes



Idk why but it still doesn’t work

my bad,

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)

Meant to say Unit instead

it still dosent work here are the outputs

EntitySpawner
  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

read the error log:

ServerScriptService.EntitySpawner.PlayerUnitHandler:38: Expected ‘end’ (to close ‘function’ at line 22), got ; did you forget to close ‘do’ at line 30?

Show line 30 and onwards please

1 Like

nevermind I can see your error from the above screenshot.
you forgot to put an end to close your function, after the ‘end)’

wait, can you show me where? I’m a little bit confused

Here in the first image specifically, you need to add an end after the end) to close the function

1 Like

like so?

	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

yes, now try run it in your studio

1 Like

you really need to start reading your errors properly

This says Unit is not a member of ServerStorage, where is it located?

i was testing it out in workspace