Hello, Folks! After some time working on a stamina/sprinting system (which I’m almost sure every beginner worked on before), I’ve finally finished it and I would like some feedback on it and what I can improve on it, specially since there’s 239 lines of code of it! Is that normal? Since I’m a beginner, I know for a fact that the code might be somewhat bad (so sorry in advance if you end up having any sort of nightmares while reading it). Below will be the place that has the code and also my thought while working on it. Any type of help is appreciated as it will help me learn a few things.
Stamina_Test.rbxl (63.5 KB)
Everything is located in the ServerScriptService
and ReplicatedStorage
. First thing first, why are the GUI’s in the ReplicatedStorage
instead of StarterGui
? Simple, it’s because of this post. May be outdated or something but, while unsure, I prefer keeping them into ReplicatedStorage
and keep a script inside there to act like StarterGui
. Secondly, I’m using a remote event to act as a remote function instead. I don’t think there’s a problem on that and I’m pretty sure I saw a certain post about it saying that some big games do that aswell. Of course, I’ll still use remote functions for important things like DataStore
and whatnot.
Alright, time to tell the process while working on it. I wanted to create a stamina system where the player has somewhat of a control of their stamina, which means the server will “trust” the stamina the client send (while still doing some checks) and allow them to begin running. There are two attributes: ClientStamina
and ServerStamina
. As obvious as it’s, the client will deplete the ClientStamina
and the server the ServerStamina
. Once the ServerStamina
reaches 0, the server wont change the player’s walkspeed back to walking. Instead, it will wait for the player fire the server and check the stamina the client sent. If they don’t respond within 7 seconds, they will be kicked. There could be a few problems like: “What if the player lags right when the server hits 0?” but not sure if that’s much of a problem; if they lagged for that long then they shouldn’t be in the server, to avoid a few cheats and whatnot. I’m also unsure if the player will be considered as moving to the server if the player lags and walk in place. That would be a problem if it did.
After the player stop running, I wanted the stamina to regen after 0.5 seconds. After checking that task.wait()
wasn’t the best way to do so, I’ve decided to use task.delay()
to schedule the regen and, if the player decided to run again while the regen was still scheduled, cancel it.
I’ve noticed that every so often there’s a certain gap from the ClientStamina
and ServerStamina
. For example, sometimes the ClientStamina
had 76 and the ServerStamina
72. To fix this issue I decided to add 2 of stamina on the server if the gap is too big. Is there a better solution? I’ve also noticed that sometimes there were some errors happening on the StaminaService
regarding the player’s character whe the player leaves during or after a run/regen loop. Added a few if
but not sure if that’s the best approach.
I’ve tried checking every way this could be exploitable but I might’ve forgotten some (or could’ve tried to do it in a better way). I also tried to check everything that a player could do, even if unlikely, to avoid errors and I managed to probably work on them all (and that was rather annoying). For example, the script would break if the player spammed sprint while low on stamina. Not sure if anything slipped by.
Lastly, I wanted the stamina bar to fade in and out whenever the player starts running with full stamina or when the player reaches max stamina respectively. I’ve tested just a few times with two players and I guess it’s working alright. Of course, disconnect connections if the player leave and also clear values.
On the line 120 of the RunScript
, I bind the action when the player spawn and unbind when the player dies. Why? To avoid the player from firing the server when they are unable to run. There’s also a random killbrick to test the loop whenever the player dies
Before finishing, for future reference, should I go “if it works, it works”? I know you can avoid a few unnecessary loops, if and whatnot but I want to be sure that there’s many ways to get the same result and none of them are wrong (unless you add too many unnecessary things), just like in modelling.
I’ll probably post two other code for feedback later aswell. One of them is a shop + inventory/item system and another a status effect script. The former is completed but I want to polish it a bit more. Anyways, thanks for your time and sorry for the huge text . Wanted to make sure y’all understood what were my thoughts while making it. Send me a question if you have any doubt on a certain piece of my code (which will probably happen)