Lag issue with game

repost due to no replies on my previous post

My script is lagging the whole server. I have found the reason why, it is due to a runservice connection being made for every zombie but I just can’t find an alternative. Pathfinding breaks my script as shown here:

PathFinding Attempt
local Waves = {}
local spawners_folder = game.Workspace.Spawners
local enemies = game:GetService("ServerStorage"):WaitForChild("Enemies")
local enemiesinworkspace = workspace.Enemies
local count = 0
function Waves.Spawn(zombie, amount, plr, char)
	local Exists = enemies:FindFirstChild(zombie)
	if Exists then

		while true do
			task.wait()
		

			local clone = Exists:Clone()
			for i,v in ipairs(clone:GetDescendants()) do
				if v:IsA("BasePart") then
					v.CollisionGroup = "Zombie"
				end
			end

			count+=1
			for i,v in  pairs(spawners_folder:GetChildren()) do
				clone.HumanoidRootPart.CFrame = v.CFrame
				clone.Parent = enemiesinworkspace
			end
			local function CodeAi()
				local zombieroot = clone.HumanoidRootPart
				local hum = char.Humanoid
				local zombiehumanoid = clone.Zombie
				local humanoidroot = char.HumanoidRootPart
				local renderservice = game:GetService("RunService")
				local PathfindingService = game:GetService("PathfindingService")				
				local path = PathfindingService:CreatePath()
				path:ComputeAsync(zombieroot.Position, humanoidroot.Position)
				local waypoints = path:GetWaypoints()
				for i,v in pairs(waypoints) do
					zombiehumanoid:MoveTo(v.Position)
					while true do
						local distance = (zombieroot.Position - v.Position).Magnitude
						
						local distance_from_player = (zombieroot.Position - humanoidroot.Position).Magnitude
						if distance >100 then
							zombiehumanoid:MoveTo(v.Position)
						end
						if distance_from_player >100 then
							zombiehumanoid:MoveTo(v.Position)

						end
						task.wait()			
					end
				end
			end
			if count == amount then
				break
			end		
			CodeAi()
		end
	else
		warn("Requested Zombie " .. zombie .. " does not exist")
	end
end


return Waves

Behaviour from pathfinding attempt:

How here is my script for the zombies that lags everything. It works but lags. It can either lag fps or the whole server through ping.
Script:
local Waves = {}

local spawners_folder = game.Workspace.Spawners
local enemies = game:GetService("ServerStorage"):WaitForChild("Enemies")
local enemiesinworkspace = workspace.Enemies
local count = 0
function Waves.Spawn(zombie, amount, plr, char)
	local Exists = enemies:FindFirstChild(zombie)
	if Exists then

		while true do
			task.wait(1)
		

			local clone = Exists:Clone()
			for i,v in ipairs(clone:GetDescendants()) do
				if v:IsA("BasePart") then
					v.CollisionGroup = "Zombie"
				end
			end

			count+=1
			for i,v in  pairs(spawners_folder:GetChildren()) do
				clone.HumanoidRootPart.CFrame = v.CFrame
				clone.Parent = enemiesinworkspace
				if count == amount then
				break
			end		
			end
			function CodeAi()
				local renderservice = game:GetService("RunService")
				local hum = clone.Zombie
				local humrootpart = char.HumanoidRootPart
					local hitbox = clone.HitBox
				local animator = hum.Animator
				local animid_kill = clone.ZombieAttack
				local animid_killgo = hum:LoadAnimation(animid_kill)
				local distance = nil
				local direction = nil
				local zombieroot = clone.HumanoidRootPart
				local debounce = false
				local Conn = renderservice.Heartbeat:Connect(function()
					local mag = (humrootpart.Position - zombieroot.Position)
					distance = mag.Magnitude
					direction = mag.Unit
					hitbox.Touched:Connect(function(hit)
						if hit.Parent:FindFirstChild("Humanoid") then
							if debounce == false then
								debounce = true
								hit.Parent.Humanoid:TakeDamage(5)
								animid_killgo:Play()
								task.wait(2)
								debounce = false
							end
						end
					end)
					if distance <=100 then
						hum:Move(direction)
					elseif distance >=100 then
						hum:Move(Vector3.new(math.random(1,50),math.random(1,2),math.random(1,50)))

					end
				end)

				plr.Character.Humanoid.Died:Once(function()
					Conn:Disconnect()
				end)
			end
coroutine.wrap(CodeAi)()
		warn("Requested Zombie " .. zombie .. " does not exist")
	end
end

end
return Waves

In client, it lags the server through ping(and when the player dies, the zombies continue spawning exceeded 16.)
In Roblox studio however, the fps drops significantly.

I can’t seem to find an alternative so this post is my last resort.

1 Like

This video might help, and Suphi’s Kord server is a lot of help as well:

3 Likes

Split it up into multiple functions then the move function where you actually move it is called to continuously run it. If that doesn’t work it could be due to the memory as you are calling the CodeAi() function in a function which I believe to be bad practice so if you just move it outside and add in parameters it should work

1 Like

You are making connections inside of the heartbeat connection, there is also no reason to coroutine.wrap this CodeAi function.

local Conn = renderservice.Heartbeat:Connect(function()
	local mag = (humrootpart.Position - zombieroot.Position)
	distance = mag.Magnitude
	direction = mag.Unit
	hitbox.Touched:Connect(function(hit) -- making a new connection every frame!
	-- move hitbox.Touched:Connect outside of the hearbeat connection

Instead of a heartbeat you could try using :MoveTo with semi-short distances

function CodeAi()
	local renderservice = game:GetService("RunService")
	local hum: Humanoid = clone.Zombie
	local humrootpart = char.HumanoidRootPart
	local hitbox = clone.HitBox
	local animator = hum.Animator
	local animid_kill = clone.ZombieAttack
	local animid_killgo = hum:LoadAnimation(animid_kill)
	local distance = nil
	local direction = nil
	local zombieroot = clone.HumanoidRootPart
	local debounce = false
	
	task.defer(function()
		while hum.MoveToFinished:Wait() do
			local direction = (humrootpart.Position - zombieroot.Position)
			local distance = direction.Magnitude
			
			if distance > 100 then
				local random_position = Vector3.new(math.random(1,50),math.random(1,2),math.random(1,50))
				hum:MoveTo(random_position + zombieroot.Position)
			else
				hum:MoveTo(humrootpart.Position)
			end
		end
	end)
	
	-- again move hitbox.Touched here, outside of the loops/heartbeat
	hitbox.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if debounce == false then
				debounce = true
				hit.Parent.Humanoid:TakeDamage(5)
				animid_killgo:Play()
				task.wait(2)
				debounce = false
			end
		end
	end)
2 Likes