How many Heartbeat events is too much?

Say I had a game capable of handling a maximum of 20 players, and each of those players could spawn a ship of their own.

Each ship contains a server script, and inside that script is the following:

local Ship = script.Parent
local Move = Ship:WaitForChild("Move")
local Seat = Ship:WaitForChild("VehicleSeat")
local BodyGyro = Move:WaitForChild("BodyGyro")
local BodyVelocity = Move:WaitForChild("BodyVelocity")
BodyGyro.CFrame = Move.CFrame
local Val = 0
local runService = game:GetService("RunService")

runService.Heartbeat:Connect(function()
	runService.Heartbeat:Wait()
	BodyGyro.CFrame = Move.CFrame
	local Speed = Seat.MaxSpeed
	local TurnSpeed = Seat.TurnSpeed
	
	if Seat.Throttle == 1 then
		if Val < Speed then
			Val = Val + 1
		end
		BodyVelocity.Velocity = Move.CFrame.lookVector * Val
	else
		if Seat.Throttle == 0 then
			Val = 0
			BodyVelocity.Velocity = Move.CFrame.lookVector * Val
		else
			if Seat.Throttle == -1 then
				if Val < 14 then
					Val = Val + 1
				end
				BodyVelocity.Velocity = Move.CFrame.lookVector * -Val
			end
		end
	end
	
	if Seat.Steer == 1 then
		BodyGyro.CFrame = BodyGyro.CFrame * CFrame.fromEulerAnglesXYZ(0, -(TurnSpeed), 0)
	else
		if Seat.Steer == -1 then
			BodyGyro.CFrame = BodyGyro.CFrame * CFrame.fromEulerAnglesXYZ(0, TurnSpeed, 0)
		end
	end
end)

In other words, that’s 20 ships (maximum) using Heartbeat.
I don’t know about you, but that seems like a bad idea.
Would this cause a significant performance drop?

2 Likes

It is only too much if it results in significant performance issues.

This can be okay if those ships are handled by 20 separate clients.

A local script is not going to move the boat though (since the script is within the boat). Any suggestions on how I would be able to make it move client-sided?

You can change your code organization by somehow letting the client handle it (also, you need to set the networkownership of the ship to the client). It really depends on how you want to use your boats, but this is probably the most elegant solution if you are worried about performance. You should generally try to leave the server alone if you can.

Ok thank you. I’ll find a way to do so.