Make a function run every X minutes on all players

I want to run a function every X minutes to affect all players (The function is an “XP bonus” sort of system that rewards players with XP, in addition of letting them know of their hunger/thirst/fatigue levels).
I suppose I can use the OnHeartbeat in combination with getting and setting local ints as a counter that I can reset, but it seems like a really backward way of doing it (let alone I’m a bit worried as to how it would affect performance). Does anyone have a better system?

Generally the module HB is the place for that. It’s not unusual to have several things that you want to do at different intervals (every hour check this, every day check this) on all the players. You can then save variables on the module for last time you ran each check and just compare.

The module HB is going to run anyway, and there’s only one of it.

Another way would be to start a pseudo-HB when the module loads that runs at the period you want to check. This should work fine. I find it a bit more cumbersome so I don’t do this sort of thing this way.

1 Like

You’re right, although for debugging and learning purposes I ran some heavy scripts on the OnHeartBeat module (a big while loop with CreateObject) and it micro-freezed every 6 seconds. So, I will try it for lighter scripts but still gotta be careful with that.