# Run experiments in game

> Assign players to experiment variants with GetVariant, how consistent assignment works, and how exposures are logged.

You create experiments in Bloxlytics ([Experiments](https://docs.bloxlytics.app/analytics/experiments)): a key, an allocation, two to six variants with weights, and an optional flat config per variant. The SDK receives running experiments through [remote configuration](https://docs.bloxlytics.app/sdk/remote-config) and assigns players in game with one call.

```luau
local variant, config = Analytics:GetVariant(player, "starting-coins")
if variant then
	coins = config.StartingCoins -- e.g. 100 or 500
end
```

## GetVariant

```luau
Analytics:GetVariant(player: Player, experimentKey: string): (string?, { [string]: any }?)
```

| Returns | When |
| --- | --- |
| `variantKey, config` | The experiment is running and the player is inside its allocation. `config` is the variant's config table from the dashboard (possibly empty). |
| `nil, nil` | The experiment isn't running, doesn't exist, the player is outside the allocation, `player` isn't a `Player`, or remote configuration hasn't loaded yet. |

`GetVariant` doesn't yield and doesn't make a request: it works entirely from the experiments the server last received. You can call it as often as you like; the answer for a player only changes if the experiment's settings change.

> **Always handle nil:** Treat `nil` as "show the normal game". Players outside the allocation get `nil` on purpose, and so does everyone in Studio without `TrackInStudio`, and anyone checked before the server's first configuration fetch has finished.

## Consistent assignment

Assignment is deterministic: the same player always gets the same variant of the same experiment, on every server, without storing anything. The SDK and Bloxlytics use the same algorithm, so the dashboard knows which variant each player saw.

Both steps use the 32-bit **FNV-1a** hash of a string.

1. **Allocation.** `bucket = fnv1a("<experimentKey>:<userId>") % 10000`. The player is in the experiment if `bucket < allocation * 100`, where `allocation` is the percentage set in the dashboard (1–100). So an allocation of 25 includes buckets 0–2499.
2. **Variant.** `pick = fnv1a("<experimentKey>:variant:<userId>") % totalWeight`, where `totalWeight` is the sum of the variants' weights. Walking the variants in order and adding up weights, the player gets the first variant whose running total is greater than `pick`.

For example, with variants `A` (weight 50) and `B` (weight 50), `pick` values 0–49 get `A` and 50–99 get `B`.

Because the allocation hash and the variant hash use different strings, raising the allocation adds new players to the experiment without moving anyone already in it between variants. Changing weights, or adding or removing variants, does reassign players, so set them before you start.

FNV-1a works on the bytes of the string: offset basis `0x811c9dc5`, and for each byte, XOR then multiply by the prime `0x01000193`, modulo 2³². Experiment keys are limited to letters, digits, `-` and `_`, so the bytes are the same in Luau and on the server. The SDK implements the multiplication with `bit32` in 16-bit halves so the result is exact.

## Exposure events

The first time `GetVariant` assigns a player to a variant during a session, the SDK sends a `$experiment_exposure` event:

| Property | Type | Value |
| --- | --- | --- |
| `experiment` | string | The experiment key. |
| `variant` | string | The variant key the player got. |

Later calls in the same session send nothing more, so call `GetVariant` wherever it is convenient. A new session (the player rejoining) logs a new exposure.

Exposures are how the experiment's results are counted: a player is in the analysis from the moment they were exposed. That means you should call `GetVariant` **at the point where the variant makes a difference**, not when the player joins. If only players who open the shop see the change, check the variant when they open the shop; otherwise players who never reached the shop dilute the result.

Nothing is logged when `GetVariant` returns `nil`, or when the player has no session (sessions turned off with `AutoTrackSessions = false` or the **Sessions** switch).

## Timing

- Servers fetch remote configuration on start-up and every 5 minutes. A newly started experiment reaches running servers within 5 minutes; a stopped one stops being assigned within 5 minutes.
- Only experiments with the status **running**, and inside their start and end dates, are sent to servers.
- The first fetch happens just after `Start`, on the SDK's own thread. Until it returns, `GetVariant` returns `nil` for everyone. If your variant decides something at join time, give the first fetch a moment, or re-check later in the session.

## Example: gating a feature

Testing whether a daily reward streak improves retention, with variants `control` (no streak) and `streak`:

```luau ServerScriptService/DailyReward.server.luau
local Analytics = require(game.ServerScriptService.Analytics)

local function onDailyRewardClaimed(player: Player, day: number)
	local variant = Analytics:GetVariant(player, "daily-streak")

	if variant == "streak" then
		grantStreakReward(player, day) -- the new feature
	else
		grantDailyReward(player) -- control, outside the experiment, or not loaded yet
	end
end
```

The exposure is logged when the player claims their first reward of the session, which is exactly when the two versions start to differ.

## Example: tuning a value

Variant config is a flat table you define per variant in the dashboard, with string, number or boolean values. Use it to test numbers without publishing:

```luau ServerScriptService/StartingCoins.server.luau
local Players = game:GetService("Players")
local Analytics = require(game.ServerScriptService.Analytics)

local DEFAULT_STARTING_COINS = 100

local function startingCoinsFor(player: Player): number
	local _, config = Analytics:GetVariant(player, "starting-coins")
	if config and typeof(config.StartingCoins) == "number" then
		return config.StartingCoins
	end
	return DEFAULT_STARTING_COINS
end

Players.PlayerAdded:Connect(function(player)
	-- Only new players are part of this experiment.
	if isFirstVisit(player) then
		setCoins(player, startingCoinsFor(player))
	end
end)
```

`isFirstVisit` and `setCoins` stand for your own data code. Check the type of each config value: if a variant was saved without a key, that key is `nil`.

## Testing in Studio

Set `TrackInStudio = true` to test experiments in Studio; without it the SDK never fetches remote configuration there, so `GetVariant` always returns `nil`. Exposures logged while you test in Studio count towards the experiment like any other, so keep tests short.
