# CodeRanch - Coin Toss & Dice Roll

**Old Western Coin Toss & Dice Roll Gambling System for RedM**

This script provides an immersive gambling system for RedM servers. Players can challenge each other to coin toss or dice roll games with customizable bets. The system features animations, sound effects, and full framework support.

## 🚀 Features

* **Multi-Framework Support**: VORP, RSG, QBR and Standalone
* **Coin Toss Gambling**: Classic heads or tails betting
* **Dice Roll Gambling**: Customizable dice with 2-20 sides
* **Player vs Player**: Challenge nearby players to wager matches
* **Animated Gameplay**: Realistic coin flip and dice roll animations
* **Sound Effects**: Immersive audio feedback for all actions
* **Interactive UI**: NUI interface for creating challenges
* **Quick Commands**: Fast challenge system via commands
* **Bet Validation**: Automatic validation of bet amounts and player funds
* **Auto-Expiry**: Challenges expire after timeout period

## 📋 Requirements

* **RedM Server** (Beta 1491+)
* **Supported Frameworks**: VORP, RSG, QBR or Standalone

## 🛠️ Installation

{% stepper %}
{% step %}

### Download Files

Copy the resource folder to your server's resources folder.

```bash
# Copy to your server's resources folder
resources/[coderanch]/coderanch-cointoss/
```

{% endstep %}

{% step %}

### Server.cfg Settings

Add your framework dependencies (only add the ones you use) and ensure the coin toss resource.

```cfg
# Framework dependencies (only add the ones you use)

# If using VORP
ensure vorp_core

# If using RSG
ensure rsg-core

# If using QBR
ensure qbr-core

# Coin Toss Script
ensure coderanch-cointoss
```

{% endstep %}

{% step %}

### Configuration

Edit `config.lua` file according to your server's needs.
{% endstep %}
{% endstepper %}

## ⚙️ Configuration

### Framework Settings

```lua
Config.Framework = 'auto' -- "auto", "vorp", "rsg", "qbr", "standalone"
```

### Game Settings

```lua
Config.MinBet = 1           -- Minimum bet amount
Config.MaxBet = 1000        -- Maximum bet amount (0 = no limit)
Config.MaxDistance = 3.0    -- Maximum distance between players
```

### Coin Toss Settings

```lua
Config.CoinToss = {
    Enabled = true,              -- Enable coin toss
    Command = 'cointoss',        -- Command to initiate
    AnimationDuration = 3000,    -- Animation duration (ms)
}
```

### Dice Roll Settings

```lua
Config.DiceRoll = {
    Enabled = true,              -- Enable dice roll
    Command = 'diceroll',        -- Command to initiate
    MinSides = 2,                -- Minimum dice sides
    MaxSides = 20,               -- Maximum dice sides
    DefaultSides = 6,            -- Default dice sides
    AnimationDuration = 3000,    -- Animation duration (ms)
    AllowMultiPlayer = true,     -- Allow multi-player
    MaxPlayers = 6,              -- Maximum players
}
```

### UI Settings

```lua
Config.UI = {
    Theme = 'western',           -- UI theme
    ShowNotifications = true,    -- Show notifications
    AcceptKey = 0x760A9C6F,      -- G key (accept)
    DeclineKey = 0x8FD015D8,     -- X key (decline)
    ChallengeTimeout = 30000,    -- Timeout (30 seconds)
}
```

### Sound Settings

```lua
Config.Sounds = {
    Enabled = true,
    CoinFlip = 'coin-flip.mp3',
    DiceRoll = 'dice.mp3',
    Win = 'win.mp3',
    Lose = 'lost.mp3',
    Volume = 0.3,                -- Volume (0.0 - 1.0)
}
```

### Animation Settings

```lua
Config.Animations = {
    Enabled = true,
    CoinProp = 'p_coinpurse02x', -- Coin prop model
    DiceProp = 'p_dice01x',      -- Dice prop model
}
```

### Language Settings

```lua
Config.Messages = {
    NotEnoughMoney = 'You don\'t have enough money for this bet!',
    PlayerTooFar = 'That player is too far away!',
    InvalidBet = 'Invalid bet amount! (Min: $%s, Max: $%s)',
    PlayerNotFound = 'Player not found!',
    AlreadyInGame = 'You or the target player is already in a game!',
    -- And more...
}
```

## 🎮 Usage

### For Players

#### Coin Toss Game

{% stepper %}
{% step %}

### Approach Another Player

Get within 3 meters.
{% endstep %}

{% step %}

### Open Challenge Menu

Type `/cointoss` (opens UI).
{% endstep %}

{% step %}

### Set Bet Amount

Enter your desired bet.
{% endstep %}

{% step %}

### Choose Heads or Tails

Pick your side.
{% endstep %}

{% step %}

### Send Challenge

Player receives a notification.
{% endstep %}

{% step %}

### Accept or Decline

Press G to accept, X to decline.
{% endstep %}

{% step %}

### Watch the Flip

Animation plays automatically.
{% endstep %}

{% step %}

### Winner Takes All

Winner receives both bets.
{% endstep %}
{% endstepper %}

Quick Challenge:

```
/cointoss [playerId] [bet] [heads/tails]
Example: /cointoss 5 50 heads
```

#### Dice Roll Game

{% stepper %}
{% step %}

### Approach Another Player

Get within 3 meters.
{% endstep %}

{% step %}

### Open Challenge Menu

Type `/diceroll` (opens UI).
{% endstep %}

{% step %}

### Set Bet Amount

Enter your desired bet.
{% endstep %}

{% step %}

### Choose Dice Sides

Select 2-20 sided dice.
{% endstep %}

{% step %}

### Send Challenge

Player receives a notification.
{% endstep %}

{% step %}

### Accept or Decline

Press G to accept, X to decline.
{% endstep %}

{% step %}

### Watch the Roll

Dice animation plays.
{% endstep %}

{% step %}

### Winner Determined

Highest roll wins (tie returns bets).
{% endstep %}
{% endstepper %}

Quick Challenge:

```
/diceroll [playerId] [bet] [sides]
Example: /diceroll 5 50 6
```

### Challenge Acceptance

* Press **G** to accept the challenge
* Press **X** to decline the challenge
* Challenge expires after 30 seconds

## 🔧 Advanced Settings

### Customizing Bet Limits

```lua
Config.MinBet = 5       -- Minimum bet increased
Config.MaxBet = 0       -- No maximum limit
```

### Adjusting Game Duration

```lua
Config.CoinToss.AnimationDuration = 5000  -- 5 seconds
Config.DiceRoll.AnimationDuration = 5000  -- 5 seconds
```

### Disabling Features

```lua
Config.CoinToss.Enabled = false   -- Disable coin toss
Config.DiceRoll.Enabled = false   -- Disable dice roll
Config.Sounds.Enabled = false     -- Disable sounds
Config.Animations.Enabled = false -- Disable animations
```

### Customizing Dice Range

```lua
Config.DiceRoll.MinSides = 4    -- Minimum 4-sided dice
Config.DiceRoll.MaxSides = 100  -- Maximum 100-sided dice
Config.DiceRoll.DefaultSides = 20 -- Default to 20-sided dice
```

### Changing Key Bindings

```lua
Config.UI.AcceptKey = 0x760A9C6F   -- G key (default)
Config.UI.DeclineKey = 0x8FD015D8  -- X key (default)
```

## 🐛 Troubleshooting

<details>

<summary>Framework Not Detected</summary>

* Set `Config.Framework` manually instead of "auto"
* Make sure your framework is properly loaded
* Check framework name in fxmanifest.lua

</details>

<details>

<summary>Money Not Deducting/Adding</summary>

* Verify framework compatibility
* Check if player has sufficient funds
* Enable debug mode: `Config.Debug = true`

</details>

<details>

<summary>Challenges Not Working</summary>

* Check players are within range (default 3 meters)
* Verify both players have enough money
* Make sure player isn't already in a game

</details>

<details>

<summary>Animations Not Playing</summary>

* Check `Config.Animations.Enabled = true`
* Verify prop models exist in game
* Test with debug mode enabled

</details>

<details>

<summary>No Sound Effects</summary>

* Check `Config.Sounds.Enabled = true`
* Verify sound files exist in `nui/sounds/` folder
* Check volume settings: `Config.Sounds.Volume`

</details>

## 🔄 Game Flow

### Coin Toss Flow

{% stepper %}
{% step %}

### Challenge Sent

Player A challenges Player B with bet amount.
{% endstep %}

{% step %}

### Notification

Player B receives notification.
{% endstep %}

{% step %}

### Acceptance

Player B accepts or declines.
{% endstep %}

{% step %}

### Deduct Bets

If accepted, both players' money is deducted.
{% endstep %}

{% step %}

### Animation

Coin flip animation plays (3 seconds).
{% endstep %}

{% step %}

### Outcome

Coin lands on heads or tails.
{% endstep %}

{% step %}

### Payout

Winner receives both bets (bet × 2).
{% endstep %}
{% endstepper %}

### Dice Roll Flow

{% stepper %}
{% step %}

### Challenge Sent

Player A challenges Player B with bet and dice sides.
{% endstep %}

{% step %}

### Notification

Player B receives notification.
{% endstep %}

{% step %}

### Acceptance

Player B accepts or declines.
{% endstep %}

{% step %}

### Deduct Bets

If accepted, both players' money is deducted.
{% endstep %}

{% step %}

### Roll

Both players roll the dice.
{% endstep %}

{% step %}

### Outcome

Higher roll wins; tie returns bets.
{% endstep %}

{% step %}

### Payout

Winner receives both bets (bet × 2).
{% endstep %}
{% endstepper %}

## 🛡️ Safety Features

* **Bet Validation**: Automatic validation of bet amounts
* **Fund Check**: Verifies both players have enough money
* **Distance Check**: Players must be within range
* **Game Lock**: Players cannot join multiple games
* **Auto-Expiry**: Challenges expire after 30 seconds
* **Self-Challenge Prevented**: Cannot challenge yourself

## 🎨 Customization

### Adding Custom Sounds

Place your custom sounds in `nui/sounds/` folder and reference them in config:

```lua
Config.Sounds.CoinFlip = 'my-custom-flip.mp3'
Config.Sounds.Win = 'my-custom-win.mp3'
```

### Modifying UI Theme

Edit `nui/css/style.css` and `nui/js/script.js` to customize the interface.

### Adding New Props

Use GTA V object hash names for custom props:

```lua
Config.Animations.CoinProp = 'prop_name_hash'
```

## 🔍 Debug Mode

Enable debug mode to see detailed console logs:

```lua
Config.Debug = true
```

Debug command (admin only):

```
/gamesdebug
```

## 📊 Framework Integration

### VORP Core

```lua
Config.Framework = 'vorp'
```

### RSG Core

```lua
Config.Framework = 'rsg'
```

### QBR Core

```lua
Config.Framework = 'qbr'
```

### Standalone

```lua
Config.Framework = 'standalone'
```

## 🔄 Updates

### v1.0.0

* Initial release
* Coin toss gameplay
* Dice roll gameplay
* Multi-framework support
* NUI interface
* Sound effects
* Animations
* Debug mode

## 📄 License

This script is developed by CodeRanch. Commercial use requires permission.

***

**Note**: This script is developed for RedM Beta version. Incompatibilities may occur in RedM's official release.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coderanch-redm-store.gitbook.io/coderanch-redm-store-docs/coderanch-coin-toss-and-dice-roll.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
