# RunService

RunService : `Instance`

## Overview

A RunService allows you to handle logic using frame-based loop events and identify whether the currently operating environment is a server, Studio, or client.

This ability to check the operating environment is particularly useful in ModuleScripts shared between the client and server, when they have to implement different behavior based on the environment.

## Properties

## Methods

### IsClient

Returns whether the code that called the method is running in the client environment.

Returns true if it is called by a LocalScript or a ModuleScript loaded by a LocalScript.

#### Parameters

#### Return

| `bool` | Specifies whether it is running in the client environment. |
| ------ | ---------------------------------------------------------- |

#### Code Samples

```lua
local RunService = game:GetService("RunService")

local IsClient = RunService:IsClient()
print("IsClient : ", IsClient)
```

### IsServer

Returns whether the code that called the method is running in the server environment.

Returns true if it is called by a server-side Script or a ModuleScript loaded by a Script.

#### Parameters

#### Return

| `bool` | Specifies whether it is running in the server environment. |
| ------ | ---------------------------------------------------------- |

#### Code Samples

```lua
local RunService = game:GetService("RunService")

local IsServer = RunService:IsServer()
print("IsServer : ", IsServer)
```

### IsStudio

Returns whether the current client is running in the Studio environment.

This is useful for conditionally running a test code that should only run in Studio.

#### Parameters

#### Return

| `bool` | Specifies whether it is running in the Studio environment. |
| ------ | ---------------------------------------------------------- |

#### Code Samples

```lua
local RunService = game:GetService("RunService")

local IsStudio = RunService:IsStudio()
print("IsStudio : ", IsStudio)
```

## Events

### Heartbeat

After the physics simulation is completed, this event is called at the final step of every frame.

This is the typical execution point for scripts, where scripts scheduled via task.delay or task.spawn are also processed at this time.

This event is ideal for logic that needs to run periodically, such as health regeneration or timer updates.

#### Parameters

| `number` deltaTime | <p>The elapsed time in seconds between the previous frame and the current frame.</p><p>Can be used for calculations based on the time interval between frames.</p> |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |

#### Code Samples

```lua
local RunService = game:GetService("RunService")
local Timer = 0

local function UpdateEvent(deltaTime)
    Timer = Timer + deltaTime
    print(Timer)
end
RunService.Heartbeat:Connect(UpdateEvent)
```

### RenderStepped

This event is called every frame before the frame is rendered on the screen.

#### Parameters

| `number` deltaTime | <p>The elapsed time in seconds between the previous frame and the current frame.</p><p>Can be used for calculations based on the time interval between frames.</p> |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |

#### Code Samples

```lua
local RunService = game:GetService("RunService")
local Timer = 0

local function OnRenderStepped(deltaTime)
    Timer = Timer + deltaTime
    print(Timer)
end
RunService.RenderStepped:Connect(OnRenderStepped)
```

### Stepped

This event is called every frame before the physics simulation begins.

#### Parameters

| `number` time      | The elapsed time in seconds since RunService started.                                                                                                              |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `number` deltaTime | <p>The elapsed time in seconds between the previous frame and the current frame.</p><p>Can be used for calculations based on the time interval between frames.</p> |

#### Code Samples

```lua
local RunService = game:GetService("RunService")

local function OnStepped(timeElapsed, deltaTime)
    print(timeElapsed)
end
RunService.Stepped:Connect(OnStepped)
```
