In my game, each player on join gets a single RunService.Heartbeat function that calls on 5 functions repeatedly throughout the game. But the function where this RunService is called from has too many lines that make it difficult to read. About half of it is dedicated to a single function.
Now I’m thinking of adding 2 RunService.Heartbeat function per each player to split that single function away from the rest. So if there’s 10 players in the game, theres 20 total RunService.Heartbeat running at all times. Likewise if there’s 20 there 40 RunService.
My question is, should I be worried about how many RunService.Heartbeat loops the server can handle? More info below…
The five functions that get called repeatedly
1: AddCpsToCash() ← spits cash into their DataStore
2: UpdateClientRegister() ← Updates the clients visual on multipliers stacked on their clicks/sec
3: CheckForActivePotions() ← Decreases timer on any active potions they have
4: IsUpgradeReady() ← Signal fires any upgrades the player unlocks with sufficient cash
5: ToBillboard() ← Updates the cash billboard on top of their characters head.
This is what I want to split off.
local function CheckForActivePotions(DeltaTime)
if Adrenite.IsRunning then
if Adrenite.Duration > 0 then
Adrenite.Duration -= DeltaTime
ClientPotionsTimer.Adrenite.Value = Adrenite.Duration
else
Adrenite.IsRunning = false
Register_Multipliers.Adrenite = 0
end
end
if Squeezy.IsRunning then
if Squeezy.Duration > 0 then
Squeezy.Duration -= DeltaTime
ClientPotionsTimer.Squeezy.Value = Squeezy.Duration
else
Squeezy.IsRunning = false
Cps_Multipliers.Squeezy = 0
end
end
if Congles.IsRunning then
if Congles.Duration > 0 then
Congles.Duration -= DeltaTime
ClientPotionsTimer.Congles.Value = Congles.Duration
else
Congles.IsRunning = false
Cps_Multipliers.Congles = 0
end
end
if CPS_Booster.IsRunning then
if CPS_Booster.Duration > 0 then
CPS_Booster.Duration -= DeltaTime
buffsTimer.CPS_Booster.Value = CPS_Booster.Duration
else
CPS_Booster.IsRunning = false
Cps_Multipliers.CPS_Booster = 0
end
end
if Clicks_Booster.IsRunning then
if Clicks_Booster.Duration > 0 then
Clicks_Booster.Duration -= DeltaTime
buffsTimer.Clicks_Booster.Value = Clicks_Booster.Duration
else
Clicks_Booster.IsRunning = false
Register_Multipliers.Clicks_Booster = 0
end
end
end
Where it gets ran:
local function Clock(DeltaTime)
--// Should the player leave, call Disconnect() on this and ClicksHandler function
if not player:IsDescendantOf(Players) then
ClockHandler[player]:Disconnect()
return
end
UpdateClientRegister()
AddCpsToCash(DeltaTime)
CheckForActivePotions(DeltaTime)
UpgLib.IsUpgradeReady(player, ProfileData)
ToBillboard()
end
ClockHandler[player] = RunService.Heartbeat:Connect(Clock)