Why is my code randomly being delayed?

For the past week or so, I have been attempting to fix this issue. For some reason, there is a part of my code that takes around ~0.7 seconds to load. I don’t know WHY it does this, or what causes it, especially since all the delays I could think of (that aren’t lag caused) are waits that are in a task.spawn().

runService.Heartbeat:Connect(function()
	if humanoid.Health <= 0 then
		return
	end
	
	local closestEntity = mobModule.FindClosestEnemy(unit.HumanoidRootPart.Position, unit.Team)
	if closestEntity then
		hrp.BodyGyro.CFrame = CFrame.lookAt(hrp.Position, Vector3.new(closestEntity.PrimaryPart.Position.X, hrp.Position.Y, closestEntity.PrimaryPart.Position.Z))
		if not jumpCooldown then
			if humanoid.FloorMaterial ~= Enum.Material.Air then
				task.spawn(function()
					jump(closestEntity)
				end)
			end
		else
			if not shockwaveCooldown and humanoid.FloorMaterial ~= Enum.Material.Air then
				shockwaveCooldown = true
				humanoid:MoveTo(hrp.Position, hrp)
				tweenSize(0.3, 1, 1, 1)

				local shockwave = replicatedStorage.Assets.Miscellanous.SlimeShockwave:Clone()
				shockwave.Color = unit.Head.Color
				shockwave.Position = unit.Head.Position - (Vector3.new(0, unit.Head.Position.Y, 0))
				shockwave.Parent = workspace.Miscellanous

				replicatedTweening:Create(shockwave, TweenInfo.new(0.6, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Size = Vector3.new(15, 0.15, 15), Transparency = 1}):Play()
				debris:AddItem(shockwave, 0.7)
			end
		end
		
		if not unit.HumanoidRootPart.Anchored and players:GetPlayerFromCharacter(closestEntity) then
			if unit.HumanoidRootPart:GetNetworkOwner() ~= players:GetPlayerFromCharacter(closestEntity) then
				unit.HumanoidRootPart:SetNetworkOwner(players:GetPlayerFromCharacter(closestEntity))
			end
		end
	end
end)

As you can see in the video, the crash onto the ground (shockwave, enemy getting shorter but thicker, and the enemy stopping in their tracks). If anybody needs more context to understand or help, I can give it!

4 Likes

Also my apologies for the low quality and slow video. I used Roblox’s bad in-built screen recorder.

1 Like

This is a tricky one, i’ve tried to cut down the code to only things that could be causing waits

runService.Heartbeat:Connect(function()
	if humanoid.Health <= 0 then
		-- no waits here
	end
	
	local closestEntity = mobModule.FindClosestEnemy(unit.HumanoidRootPart.Position, unit.Team)
	if closestEntity then
		if not jumpCooldown then
			-- no waits here
		else
			if not shockwaveCooldown and humanoid.FloorMaterial ~= Enum.Material.Air then
				tweenSize(0.3, 1, 1, 1)
				debris:AddItem(shockwave, 0.7) -- Not a wait but this is probably related
			end
		end
		
		if not unit.HumanoidRootPart.Anchored and players:GetPlayerFromCharacter(closestEntity) then
			-- Maybe??
			-- This could be causing the slime to freeze midair and not land properly.
			if unit.HumanoidRootPart:GetNetworkOwner() ~= players:GetPlayerFromCharacter(closestEntity) then
				unit.HumanoidRootPart:SetNetworkOwner(players:GetPlayerFromCharacter(closestEntity))
			end
		end
	end
end)

My only guess is that setnetworkownership part is causing the slime to ‘freeze’ in midair due to strange replication bugs.
(for example: i’ve noticed that dead players with setnetworkowner(nil) applied to them will freeze entirely client side while inside any part, even if it’s non tangible)

Try tinkering with not spawning a shockwave or removing setnetworkowner from the code.

Hmm, I think I’ll try the SetNetworkOwner thing soon. I had another issue with an enemy using SetNetworkOwner where it just repeatedly bounced up and down when it belonged to a player.

1 Like

I wouldn’t touch setnetworkowner (for this case), as roblox handles it automatically for you when an unanchored part is near a player. Changing it manually disables the automatic handling for that part, and causes the odd behavior you just mentioned.

1 Like

I mean I’ll see what happens if I disable it, unless I am misunderstanding what you’re saying

image
This is what it looked like when I made the SetNetworkOwner part a comment. The same thing happens. I think I’ll keep attempting to mess around with the script until I find a solution.

Alright, try setting the despawn time for the shockwave to 0 or a much higher value like 5 or something. If that doesn’t reveal anything it’s probably one of the functions not included in the code snippet :+1:

I was thinking it might be in the jump() function, but that is only used when the shockwave isn’t being used.

local shockwaveCooldown = true
local jumpCooldown = false
function jump(entity)
	shockwaveCooldown = false
	jumpCooldown = true
	
	-- // Code
	
	tweenSize(0.5, 1.15, 0.85, 1.15)
	task.wait(0.5)
	tweenSize(0.5, 0.8, 1.25, 0.8)
	
	if not entity then
		return
	end
	
	-- // Jumping
	
	humanoid.Jump = true
	
	local raycast = workspace:Raycast(unit.PrimaryPart.Position, CFrame.lookAt(unit.PrimaryPart.Position, entity.PrimaryPart.Position).LookVector * 1500, mobModule.raycastParams)
	if raycast and raycast.Instance and raycast.Instance.Parent:IsDescendantOf(entity) then
		unit.Humanoid:MoveTo(entity.PrimaryPart.Position + Vector3.new(math.random(-3.5, 3.5), 0, math.random(-3.5, 3.5)))
	else
		local path = ezPathfinding.new(unit, entity.PrimaryPart.Position + Vector3.new(math.random(-3.5, 3.5), 0, math.random(-3.5, 3.5)))
		local waypoints = path:ReturnWaypoints()
		if waypoints[1] then
			task.wait(0.1)
		end
	end
	
	task.wait(math.random(10, 14) / 10)
	if jumpCooldown then
		jumpCooldown = false
	end
end

Regardless, I think I’ll keep trying to fix this for now.

1 Like

If you think that’s it, then replacing jump() with task.spawn(function() jump() end) will do the trick.

image
Unfortunately it already is

I’ve also tried to determine how long it takes for the shockwave part to run. It almost always says around 0.1s, but it visually takes longer than that. :person_shrugging:

Oh, sorry :sob:
It’s time to pull out the big guns then
put this code whereever it seems fit (preferably outside an infinite loop so studio doesn’t crash), and it should output a constant stream of data that’ll help you narrow the cause down

task.spawn(function() 
while true do
print("Floormaterial: "..humanoid.FloorMaterial))
-- whatever else you want
task.wait()
end
end)

The delay between the object when it hits (for the FloorMaterial) seems to be almost zero. It also doesn’t seem like its being caused by anything in the shockwave script itself-- considering that the effects of the object falling occur at the same time.

So the issue you’re having is that the shockwave isn’t timed correctly?

I’ve watched the video a few times but your NPC is doing exactly as you coded,

I mean the shockwave is intended to start exactly when the NPC hits the ground, but it is delayed for a bit and I have no idea why. Unless I’m just dumb, the cause seems to be unapparent.

Ah, i have an idea.
Try reducing the jump cooldown or removing it entirely.
I believe the slime is jumping, waiting for the jump cooldown, THEN doing the shockwave.

Oh, if you watched your video, you’d see that when you spawn the NPC, the shockwave immediately gets activated, but that’s just because you set the debounce to false when the jump function is started.

function jump(entity)
	shockwaveCooldown = false
	jumpCooldown = true
	
	-- // Code

Try moving the shockwaveCooldown all the way

	else
		local path = ezPathfinding.new(unit, entity.PrimaryPart.Position + Vector3.new(math.random(-3.5, 3.5), 0, math.random(-3.5, 3.5)))
		local waypoints = path:ReturnWaypoints()
		if waypoints[1] then
			task.wait(0.1)
		end
	end

	shockwaveCooldown = false -- < HERE
	task.wait(math.random(10, 14) / 10)
	if jumpCooldown then
		jumpCooldown = false
	end
end

To there

Okay, so I’ve been doing a bit more testing… It still hasn’t worked. I have tried modifying it a lot and no new changes.

Why don’t you benchmark certain code sections to see which takes longer? Also, you should probably probably run your code under while task.wait do and also use Humanoid.MoveToFinished:Wait()