API ReferenceCounters
Decrement
Atomically decrement a numeric value
Syntax
Redix.Decrement(key, amount, callback)Parameters
Prop
Type
Returns
Prop
Type
Examples
Basic Decrement
-- Decrement by 1
Redix.Decrement('player:123:lives', 1, function(err, newValue)
if not err then
print('Lives remaining:', newValue)
end
end)Custom Amount
-- Decrement by specific amount
Redix.Decrement('server:slots', 5)Without Callback
-- Decrement inventory count
Redix.Decrement('inventory:item:bread', 1)Use Cases
Lives System
function OnPlayerDeath(playerId)
Redix.Decrement('player:' .. playerId .. ':lives', 1, function(err, livesRemaining)
if not err then
if livesRemaining <= 0 then
TriggerEvent('player:gameOver', playerId)
else
TriggerClientEvent('updateLives', playerId, livesRemaining)
end
end
end)
endServer Slots Management
function OnPlayerLeave(playerId)
Redix.Decrement('server:active_players', 1, function(err, activePlayers)
if not err then
print('Active players:', activePlayers)
TriggerClientEvent('updatePlayerCount', -1, activePlayers)
end
end)
endInventory Management
function RemoveItem(playerId, item, quantity)
local key = 'inventory:' .. playerId .. ':' .. item
Redix.Get(key, function(err, current)
if not err and current and current >= quantity then
Redix.Decrement(key, quantity, function(err, newQty)
if not err then
TriggerClientEvent('updateInventory', playerId, item, newQty)
end
end)
else
print('Not enough items')
end
end)
endBest Practices
Check Before Decrement
function SafeDecrement(key, amount)
Redix.Get(key, function(err, current)
if not err and current then
if current >= amount then
Redix.Decrement(key, amount)
return true
else
print('Insufficient value')
return false
end
end
end)
endPrevent Negative Values
-- Check and prevent going below zero
Redix.Get('player:health', function(err, health)
if health > 0 then
Redix.Decrement('player:health', damage)
else
print('Already at zero')
end
end)