Lag Reduction/Stress reduction

Hello Developers

I have recently made a kind of “AI” for a dragon rig i created, and after looking into it it causing extreme ping spikes (from 30-40 average to 70-100 constant) And i think i know why but have no idea for an alternative.

game:GetService("RunService").Stepped:Connect(function()
	wait()
	if state == "Sleeping" then
		local torso = findTorso(root.Position)
		if torso ~= nil then
			state = "Waking Up"
			wakeup()
		end
	
	else if state == "Awake" then
			local torso = findTorso(root.Position)
			if torso ~= nil then
				root.Anchored = true
				local yOnly = Vector3.one - Vector3.yAxis
				root.CFrame = CFrame.lookAt(root.Position * yOnly, torso * yOnly) * CFrame.new(0,38.5,0)
			end
		end
	end
end)

Ive never been great with lag management so some tips would also be nice lmao

This is in a server script by the way. like everything including the moves
heres an example of what happens.
sadly didnt record the ping spike though your gonna have to trust me i did like 10 tests all with same outcome

2023-03-12 13-31-45 (the video of the moves and how it works i guess?)

There’s no point to using wait() inside of a stepped loop since the whole point of using stepped is to update a given function per update cycle. Also, else if should be written as elseif.

Do you have to do all of these updates per frame? From what I can see, you’ve got your sleeping state and Awake state, however the only bit of code which “needs” to be ran each frame (not really, but given the code) is the line root.CFrame = CFrame.lookAt(root.Position * yOnly, torso * yOnly) * CFrame.new(0,38.5,0).

The way I’d suggest you improve the code is by taking mostly everything outside of the stepped event, getting rid of code redundancy in places where you’re checking for the torso since that can be done in one step and only checking every x seconds. I see that you use a function wakeup() to change the state - could you not put the Awake code into that function, since theoretically the dragon would be awake by then?

1 Like

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