Loop still runs on character that isn't in a specific folder

Hi, I have been having problems with my own code that has gotten really confusing for me.

It’s a script where it handles all of the towers and mobs, it runs pretty well and all, until I spawn a bit of zombies… When I spawn a few of zombies, they move fine for about 10 seconds before starting to bug and stop moving.

local Loop = nil

Loop = task.spawn(function()
	while true do
		for name, tower in pairs(workspace:WaitForChild("Towers"):GetChildren()) do
			if script.Towers:FindFirstChild(tower.Name) and tower:FindFirstChildOfClass("Humanoid") then
				local humanoid = tower:FindFirstChildOfClass("Humanoid")
				
				if humanoid.Health > 0 then
					spawn(function()
						UpdateHealth(tower)

						require(script.Towers:FindFirstChild(tower.Name)).Main(tower)
					end)
				end
			end
		end
		
		for name, mob in pairs(workspace:WaitForChild("Mobs"):GetChildren()) do
			if script.Mobs:FindFirstChild(mob.Name) and mob:FindFirstChildOfClass("Humanoid") then
				local humanoid = mob:FindFirstChildOfClass("Humanoid")

				if humanoid.Health > 0 then
					spawn(function()
						UpdateHealth(mob)

						require(script.Mobs:FindFirstChild(mob.Name)).Main(mob)
					end)
				end
			end
		end
		
		task.wait()
	end
end)

Forgot to mention, when the mob dies, it’s parent is set to the ServerStorage and put into a folder. Even if I do that, somehow, someway, the loop still runs for the zombie even though it isn’t in the mobs folder.

The module script main function only moves the zombie and makes the zombie attack, thats all it does, it doesn’t have any loops or anything…

If you have any questions about my script, I can answer them.

https://files.catbox.moe/ikqhkg.mp4 – video of the bug.

1st of all, use variables man. You don’t need to build the path to the object or whatever every time the loop runs again.

local TowerFolder = workspace:WaitForChild("Towers")
local ScriptTower = script.Towers

local MobsFolder = workspace:WaitForChild("Mobs")
local ScriptMobs = script.Mobs

2nd, you should use task.spawn() rather than spawn(), you did it before, not really sure why you aren’t doing it again.


Now onto solving your problem.

It starts to lag once enemies start spawning because you’re running the loop pretty much every frame. task.wait() with no parameters waits until the next frame of the game which is about 1/60th a second. In short, you’re making the enemies move and checking their stats and all that every 1/60th a second which is gonna result in lag. So instead of doing task.wait() do task.wait(.25) or a higher number to wait a specified amount of time.

The enemies also continue to move because of this:

spawn(function()
	UpdateHealth(tower)

	require(script.Towers:FindFirstChild(tower.Name)).Main(tower)
end)

Since you’re spawning the function, it’s basically running on a separate thread unrelated to the loop. And unless the spawn function is moving the enemy 1 stud towards the player or something and not using Humanoid:MoveTo() or anything of the likes of that, the enemy will continue to move.

Also, as the loop runs, the tower’s require move function or whatevers in it it will keep getting called, causing the enemy to have multiple move functions running at once, which also adds to the lag.


How do you fix this? Well its a bit complicated since I literally only have a snippet of the code. But the first thing I’d do is stop using a loop, from the looks of the loop, it’s really not needed. What you should (probably) do is that every time an enemy is added to the folder (TowerFolder.ChildAdded()) run the enemies stuff or whatever, allowing you to not need the the task.spawn’s and makes it only run once. And every time the enemy’s health changes to zero, stop enemy’s ai from running.

Humanoid.HealthChanged:Connect(function(curHealth)
	if curHealth <= 0 then
		-- end enemy's move and stuff
	end
end)

How you do all this is up to how you currently have the game setup, since I don’t have your code, nor do I want it, I can’t really tell you specifically how to fix it.

1 Like

Hey there! Can u tell me how your script should work? I can maybe try to help if I can

It looks fine however can I take a look at the module script for the movement of mobs you’re using

Hey, thanks for replying.

Thanks for telling me to use variables, since most of the time, I don’t use it because I am a bit too lazy… So that’s why I have a history of making my codes very dirty and unreadable. (Also with the task.spawn and spawn)

With the while loop, I’ll definitely find a better solution with this, since I thought I could just make the game smooth with the while loop and not need to worry with anything, which basically got me wrong lol.

Thanks for helping out with this, I’ll definitely find a better way to do this instead of using a loop.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.