API ReferenceCounters
Increment
Atomically increment a numeric value
Syntax
Redix.Increment(key, amount, callback)Parameters
Prop
Type
Returns
Prop
Type
Examples
Basic Increment
-- Increment by 1
Redix.Increment('player:123:kills', 1, function(err, newValue)
if not err then
print('New kill count:', newValue)
end
end)Custom Amount
-- Increment by specific amount
Redix.Increment('player:123:score', 100)Without Callback
-- Increment without callback
Redix.Increment('stats:page_views')Use Cases
Player Kills Tracking
function OnPlayerKill(killerId)
Redix.Increment('player:' .. killerId .. ':kills', 1, function(err, newKills)
if not err then
TriggerClientEvent('updateKills', killerId, newKills)
if newKills == 100 then
TriggerEvent('achievement:unlock', killerId, 'killer_100')
end
end
end)
endServer Statistics
-- Track total connections
function OnPlayerConnected()
Redix.Increment('server:total_connections')
end
-- Track active players
function OnPlayerJoined()
Redix.Increment('server:active_players')
endRate Limiting
function CanPerformAction(playerId)
local key = 'ratelimit:' .. playerId
Redix.Get(key, function(err, count)
if not count then
Redix.Save(key, 1, 3600)
return true
elseif count < 100 then
Redix.Increment(key)
return true
else
print('Rate limit exceeded')
return false
end
end)
endBest Practices
Initialize Counters
-- Explicit initialization
Redix.Save('player:' .. playerId .. ':score', 0)
Redix.Increment('player:' .. playerId .. ':score', 10)Always Use Atomic Operations
-- Good - atomic operation
Redix.Increment('counter', 1)
-- Bad - not atomic, race condition possible
Redix.Get('counter', function(err, value)
value = value + 1
Redix.Save('counter', value)
end)Use Expiration for Temporary Counters
-- Daily quest counter with 24-hour expiration
Redix.Save('daily:quest:' .. playerId, 0, 86400)
Redix.Increment('daily:quest:' .. playerId)