Creating a secure sprinting system

I am currently working on a sprinting system that uses stamina so the player cannot run infinitely and must occasionally pause for a break.

however, i am not sure how to make sure exploiters can’t change their stamina value locally and have infinite stamina, an issue that games such as criminality are plagued with.

if someone could provide a flowchart for what script does what in a system like this, i would really appreciate it. (i would love it was also performance effective)

8 Likes

A flowchart would go about like that:

                  +----------------+
                  |    Initialize  |
                  +----------------+
                           |
                           v
                  +----------------+
                  |     Update     |
                  +----------------+
                           |
                           v
      +-----------------------------------+
      |         Decrease Stamina          |
      +-----------------------------------+
                           |
               +-----------+-----------+
               |                       |
               v                       v
+-----------------------+   +------------------------+
| Check Stamina         |   | Regenerate Stamina     |
+-----------------------+   +------------------------+
               |                       |
               v                       |
    +------------------+               |
    |     Exhausted    |<--------------+
    +------------------+

Initialize sets up the initial stamina value and other necessary variables when the player spawns or joins the game.

Update runs continuously, usually in a game loop or through a RunService event. It handles the main logic for stamina management.

Decrease stamina subtracts a certain amount of stamina based on the player’s actions, such as sprinting or using abilities. The amount can be determined by factors like distance covered or time spent sprinting.

Check stamina checks the current stamina value. If it’s above zero, the player has stamina remaining, and the flow continues. If it’s zero or less, the player is exhausted and cannot perform actions that require stamina.

Exhausted handles what happens when the player is exhausted. This can include disabling sprinting or other abilities until stamina is regenerated.

Regenerate stamina allows the player’s stamina to gradually replenish over time when they are not using stamina consuming actions. This can involve adding a certain amount of stamina at regular intervals.

Hope this helps boo :kiss:

6 Likes

will the stamina checks happen on the server?

2 Likes

Yes boo the stamina checks should happen on the server to prevent local manipulation :ribbon: :kiss: :heart:

2 Likes

can you maybe edit the chart to show what happens on the client or server?

2 Likes

Sure thang babes lemme do it quickly x

                 +-------------------+
                 |   Initialize      |
                 +-------------------+
                           |
                           v
   +-------------------------------+
   |      Update (Client)          |
   +-------------------------------+
                           |
                           v
      +--------------------------+
      |   Decrease Stamina (Client)|
      +--------------------------+
                           |
                           v
   +-------------------------------+
   |    Request Stamina Update     |
   |        (Client to Server)     |
   +-------------------------------+
                           |
                           v
     +--------------------------+
     |    Check Stamina (Server) |
     +--------------------------+
                           |
               +-----------+-----------+
               |                       |
               v                       v
+-----------------------+   +------------------------+
|  Insufficient Stamina |   | Sufficient Stamina     |
|     (Server)          |   |       (Server)         |
+-----------------------+   +------------------------+
                           |
                           v
   +-------------------------------+
   |     Notify Client (Result)    |
   +-------------------------------+
                           |
                           v
   +-------------------------------+
   |      Update (Client)          |
   +-------------------------------+
                           |
                           v
      +--------------------------+
      |   Regenerate Stamina (Server) |
      +--------------------------+
                           |
                           v
   +-------------------------------+
   |      Update (Client)          |
   +-------------------------------+
2 Likes

so while the player sprints will the stamina be checked on the server using a while loop or what

1 Like

Oh no babes it is not advised to use a while loop on the server to repeatedly check the player’s stamina while they are sprinting because it may have a negative effect on performance. You could alternatively set up a system where the client periodically asks the server to update the stamina value, I’ll send a modified flowchart immediately xx

1 Like
                 +-------------------+
                 |   Initialize      |
                 +-------------------+
                           |
                           v
   +-------------------------------+
   |      Update (Client)          |
   +-------------------------------+
                           |
                           v
      +--------------------------+
      |   Decrease Stamina (Client)|
      +--------------------------+
                           |
                           v
   +-------------------------------+
   |    Request Stamina Update     |
   |        (Client to Server)     |
   +-------------------------------+
                           |
                           v
     +--------------------------+
     |    Update Stamina (Server) |
     +--------------------------+
                           |
               +-----------+-----------+
               |                       |
               v                       v
+-----------------------+   +------------------------+
|  Insufficient Stamina |   | Sufficient Stamina     |
|     (Server)          |   |       (Server)         |
+-----------------------+   +------------------------+
                           |
                           v
   +-------------------------------+
   |     Notify Client (Result)    |
   +-------------------------------+
                           |
                           v
   +-------------------------------+
   |      Update (Client)          |
   +-------------------------------+
1 Like

so let me double check here

  1. set up values and stuff for the system
  2. update the values on the client side
  3. decrease the stamina when sprinting, jumping etc,
  4. update the server with the stamina usage
  5. update the values on the server
  6. send back to the server if they have enough stamina
  7. update again
2 Likes

Almost there! Let me clarify the steps based on your description x

  1. Set up values and variables
  2. Client: update visual representation
  3. Client: decrease stamina locally
  4. Client: send request to server to update stamina
  5. Server: validate request and check stamina
  6. Server: deduct stamina and update value
  7. Server: send response to client
  8. Client: receive response and handle accordingly
  9. Client: update visual representation
1 Like

how often should the stamina be updated (this game will have a maximum of 35 players per server

1 Like

The frequency at which the stamina should be updated depends on several factors, including the specific requirements and gameplay dynamics of your game

1 Like

its going to be a combat game so it will need to be pretty accurate

1 Like

Generally I’d say a reasonable update frequency for stamina values could be around 1 to 4 times per second. In a combat game where stamina accuracy is crucial, it is recommended to implement a higher update frequency (4-10 times per second or higher), I also think you can utilize physics and collision systems for precise stamina calculations, optimize network bandwidth, and gather player feedback to ensure a responsive and accurate stamina system, or just trust your own opinion about whether or not the stamina regeneration frequency should or shouldn’t stay at what you’re setting it to :kiss: :heart: :ribbon:

2 Likes

the client will handle the majority of the stamina calculations and the server is just there to prevent hackers so maybe it isnt that crucial and maybe just around 2 times a secound will detect if someone is using more stamina then they should be able to

2 Likes

Yass a lower update frequency of around 2 times per second can be sufficient to detect and prevent excessive stamina usage in that case, however it is still important to strike a balance between accuracy and performance based on your game’s specific requirements and mechanic x

1 Like

Unfortunately, server siding features like this doesn’t ensure an Exploiter won’t be able to bypass it. As well as it’ll introduce latency to your sprinting.

You could check their WalkSpeed on the server and compare their stamina and kick if they’re going too fast for their stamina, but that’s an easily flawed system since BodyVelocity sprints are the most commonly used.

You could check their actual velocity’s magnitude and then compare stamina, but that’s a more costly check than you’d think when you’re doing it for multiple players. However, it does circumvent changing WalkSpeed as well as BodyVelocity sprints.

Ideally, you should leave movement mechanics up to the client and moderate exploiters rather actively. It’s hard to strike a balance and find the time/manpower for such moderation, but it’s also better to have a smooth system like this.
Some games that have server-based sprints have a windup to their sprint to sort of mask the server’s delay, but the initial delay to when it starts is still there.

4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.