Question about using for loops in RunService events

Hello!
Lets say I have a model with 8 parts and these parts contain a SurfaceGui with a TextLabel. On every heartbeat or frame I want to change the Text of the TextLabels to display the current time for each part using a for loop.

Would doing this be bad performance wise?

Probably don’t have the most experience with this but, the performance impact between a single Heartbeat looping through and setting all the TextLabels compared to multiple heartbeats for each should be fairly negligible. All the Heartbeat event does is call the connected function(s) assigned to it each frame after the physics simulation any ways… Also, dependent on how you’re setting up this timer, you may want to consider utilizing while loops. Now, I know some people will read this and go, “Oh god what is he SAYING right now bro…” Now, they do have a point that I am crazy but, depending on if you need micro-precise updates or not, this could actually save you on performance if paired with threads.
With heartbeat:

-- pseudo code
Heartbeat:Connect(function()
	updateTextLabels() -- updates my TextLabels after the physics simulation *every* frame
end)

Without heartbeat(kinda):

-- for a constantly running timer
task.spawn(function()
	while true do
		updateTextLabels() -- updates my TextLabels every ~1 second after they are initially set...
		task.wait(1) -- internally uses heartbeat
	end
end)

-- for a timer that lasts a set interval
task.spawn(function()
	local et = 0 --elapsed time
	while et <= 7 do -- meant to last 7 seconds
		updateTextLabels() -- updates my TextLabels every ~1 second after they are initially set...
		et += task.wait(1) -- task.wait() actually returns a number of time elapsed
	end
end)
1 Like

Well I’m wanting to use it for a train with a passenger information display system. There are 8 interior PIDs and 6 exterior PIDs and there are a few different modes.

Mode 1: If the ServiceType is All Stops or Non Stop then the PIDs will say All Stops or Non Stop for about 2 seconds and then it’ll show the destination/last stop and then repeat it basically till the trains reached its destination. This mode will always show on the exterior PIDs and the interior PIDs before the train starts its service to its destination unless its an express service then it’ll show Mode 2.

Mode 2: If the service type is an express service, if the text is over a certain length then it’ll show the destination/last stop for about 2 seconds then it’ll animate the express service text right to left and repeat it basically till the trains reached its destination. This mode will always show on the exterior PIDs throughout the whole trip and will show on the interior PIDs before the train starts its service to its destination unless its an all stops or non stop service then it’ll show Mode 1.

Mode 3: Only for the interior PIDs, while the train is moving it’ll say Next Station wait 2 seconds then it’ll say the stations name for 8 seconds and then it’ll show the time (24H:M:S) for 8 seconds then repeat this until its approaching a station then it’ll show Mode 4.

Mode 4: Also only for the interior PIDs, when approaching the next station the PIDs should say Next Station for about 2 seconds then say the stations name until its left the station platform then it’ll continue to show Mode 3 again.

Mode 5: Again only for the interior PIDs, just after leaving the first station, the PIDs should animate a welcome message (“Welcome aboard this service to StationName - ServiceType”) from right to left about 3 times (There’s a sound welcoming passengers and once it stops this animation should stop and it should go for enough time to let the animation happen 3 times). Once its stopped it should start Mode 1 or Mode 2

I think I’ve explained all of the modes, but what would be the best method to update the text of multiple PIDs at once and repeated amount of times.

Seems like multiple systems that would eventually come together to be honest… I don’t necessarily know the best solution for this but, you could potentially separate each mode into their own separate functionalities so it doesn’t over complicate things.

1 Like

I feel like its better to just use like a heartbeat event and loop through each PID and set the text to whatever.

1 Like

Yes but, it may not be THAT performant now that I’m reading this… also, some of these wouldn’t even need heartbeat loops unless ALL PIDs can cycle between all 5 modes.

1 Like

What may not be performant? Your while loop idea or my heartbeat/for loop idea?

What do you mean by that?

What modes are exclusively exterior and which ones are exclusively interior? Also I meant for the heartbeat due to you having to check for what mode each PID should be in.

nvm im blind you say them in the modes

Yeah, I don’t think there’s any exclusive to exterior but interior yes. There might be but I have to think deeply about that which I can’t be bothered thinking right now lol.

Well there’s a few parts that the train couplers will touch that will trigger things and change things.

Yea that’s fair… with a system like this it’s a pain to come up with a good, let alone great, solution LOL. I’m just worried about you potentially getting yourself into a corner due to having a fixed heartbeat loop when you could potentially utilize event-based methods for some modes. Mainly for proximity based-modes
Sorry that I can’t give you a straight forward solution, this is one of the many things I have yet to do on this platform it seems. :sweat_smile:

Haha, I’ll be fine (I hope and think). This train system is very complex and have a lot of things that real life would have and unlike most train systems on Roblox, they don’t have much of these things I’ve made. Its got like millions of systems in 1 lol

Heres a thing about it

Train System
  • Coupling and uncoupling (Make trains longer or shorter)
  • Switch Tracks (Switch between trains)
  • Side Passenger Doors (Open, Close when a player presses a button, a few extra buttons for the train crew to use, lights to display weather the doors unlocked or not and isolated or not)
  • Windscreen wipers
  • End Crew Doors (Doors at the end of each carriage for the train crew to walk between multiple carriages and to exit the train in a single track tunnel)
  • An extensive signaling system
  • Lights (Ditchlights, taillights, headlights, door lights)
  • A loud and quiet horn (Loud horn and a whistle)
  • Train ignition (To start and stop the train like a car | on/off)
  • Track modelling (Model rails, sleepers, curved tracks, switch tracks)
  • Emergency brake (Stops the train faster and disables throttle)
  • Vigilance system (If the driver hasn’t interacted with the train for 30 seconds then it’ll stop the train)
  • A realistic gangway (The bouncy part in the middle of a dual carriage unit, may need bones and things in blender)
  • A pantograph that can raise and lower (The thing that gets power from the overhead wires)
  • Overhead wires and poles
  • Model signals, A light, Restricted speed light and low speed light
  • Sounds