Would my server start lagging from too many runservices?

Hello! So recently, I found a script on the devforum, which makes the player’s character lean right or left, depending on which way the character is moving. I improvised it, and made it lean forwards and backwards too, when the player moves forwards on backwards. But i wanted to make it a server script, so everyone can see the leaning, since instead of using animations, this uses the HumanoidRootPart joint to move the character.

It’s a server script located in game.StarterPlayer.StarterCharacterScripts and it has a game:GetService("RunService").Stepped in it, which is just a while true do but faster, as far as I know.

So this script goes into every player character, and I was wondering, if it would make my game lag if there were too many players.

Edit: Here’s the whole script

HumanoidRootPart = script.Parent.HumanoidRootPart
TweenService = game:GetService("TweenService")
local AngleRight = 0
local AngleForward = 0
local OriginalC0 = HumanoidRootPart.RootJoint.C0
local Direction, Velocity
tweeninfo = TweenInfo.new(0.05, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)

game:GetService("RunService").Stepped:Connect(function()
	Velocity = HumanoidRootPart.Velocity*Vector3.new(1, 0, 1)
	if Velocity.Magnitude > 2 then --Checks if the player is moving
		Direction = Velocity.Unit --Makes the velocity a vector3 instead of a CFrame for less complications
		AngleRight = HumanoidRootPart.CFrame.RightVector:Dot(Direction)/4.5
		AngleForward = HumanoidRootPart.CFrame.LookVector:Dot(Direction)/4.5
		
	else
		AngleRight = 0
		AngleForward = 0
	end
		local tween = TweenService:Create(HumanoidRootPart.RootJoint, tweeninfo, {C0 = OriginalC0*CFrame.Angles(AngleForward, -AngleRight, 0)})
		tween:Play()
end)

Probably not.

The alternative is to have a single server script that loops through all the players and connects to their CharacterAdded event and adds them to a table, and then loops through that table every render step, or something like that.

That might technically be faster, but I can’t be sure. The difference is mostly likely undetectable even for hundreds of players.

What you’re actually doing inside the event is way more important, in either case.

the correct way to do this is in a localscript and each player should only tilt there own character

and i would do this in heartbeat not stepped

stepped fires before the physics simulation

where heartbeat fires after the physics simulation so that would be a better time to tilt the character

but to answer you question of does runservice make your game lag the answer is no but it can you should only use any loops if you have to but for tilting you have no choice but to update the tilt every frame

1 Like

False. A while true do loop with zero yielding runs as fast as it can. task.wait(), .Stepped, .RenderStepped, and Heartbeat all have the same yielding duration, just offset by different amounts based on the process it’s for. In other words, while task.wait() do or while true do task.wait() is just as fast as any of the aforementioned RunService events

To answer your question, it depends on how light or resource-heavy the frame-by-frame system is.

Also, I believe animations should be performed on the client-side instead? Players have network ownership over their own characters meaning that any animations would replicate fine. Each player would be responsible for their own characters and they don’t need to deal with other player’s character animation

1 Like