API ReferenceCounters
IncrementFloat
Atomically increment a floating-point numeric value
Syntax
Redix.IncrementFloat(key, amount, callback)Parameters
Prop
Type
Returns
Prop
Type
The result is returned as a string to preserve precision. Convert to number
using tonumber() when needed.
Examples
Basic Float Increment
-- Increment by decimal amount
Redix.IncrementFloat('player:123:rating', 0.5, function(err, newValue)
if not err then
print('New rating:', tonumber(newValue))
end
end)Decrement with Negative Value
-- Decrement by using negative value
Redix.IncrementFloat('player:123:health', -25.5)Currency Management
-- Update balance
Redix.IncrementFloat('player:123:money', 150.75)Use Cases
Player Rating System
function UpdateRating(playerId, change)
local key = 'player:' .. playerId .. ':rating'
Redix.IncrementFloat(key, change, function(err, newRating)
if not err then
local rating = tonumber(newRating)
TriggerClientEvent('updateRating', playerId, rating)
if rating >= 2000 then
TriggerEvent('player:rankUp', playerId, 'Diamond')
end
end
end)
end
-- Usage
UpdateRating(playerId, 15.5) -- Won match
UpdateRating(playerId, -10.2) -- Lost matchVirtual Currency
function AddMoney(playerId, amount)
Redix.IncrementFloat('player:' .. playerId .. ':money', amount, function(err, newBalance)
if not err then
local balance = tonumber(newBalance)
TriggerClientEvent('updateMoney', playerId, balance)
end
end)
end
function RemoveMoney(playerId, amount)
Redix.IncrementFloat('player:' .. playerId .. ':money', -amount, function(err, newBalance)
if not err then
local balance = tonumber(newBalance)
if balance < 0 then
Redix.IncrementFloat('player:' .. playerId .. ':money', amount)
print('Insufficient funds')
return false
end
TriggerClientEvent('updateMoney', playerId, balance)
return true
end
end)
endHealth System with Decimals
function ApplyDamage(playerId, damage)
local damageMultiplier = GetPlayerArmorMultiplier(playerId)
local actualDamage = damage * damageMultiplier
Redix.IncrementFloat('player:' .. playerId .. ':health', -actualDamage, function(err, newHealth)
if not err then
local health = tonumber(newHealth)
if health <= 0 then
TriggerEvent('player:death', playerId)
else
TriggerClientEvent('updateHealth', playerId, health)
end
end
end)
endBest Practices
Convert String Result
-- Always convert to number when needed
Redix.IncrementFloat('score', 12.5, function(err, result)
local score = tonumber(result)
if score > 100 then
print('High score!')
end
end)Precision Handling
-- Be aware of floating-point precision
Redix.IncrementFloat('precise:value', 0.1)
Redix.IncrementFloat('precise:value', 0.2)
-- Result might be 0.30000000000000004
-- For currency, consider storing cents as integers
Redix.Increment('player:money:cents', 150) -- $1.50Validate Before Decrement
function SafeDecrement(key, amount)
Redix.IncrementFloat(key, 0, function(err, current)
local value = tonumber(current)
if value >= amount then
Redix.IncrementFloat(key, -amount)
return true
else
print('Insufficient balance')
return false
end
end)
endPerformance
- Time Complexity: O(1)
- Atomic: Yes, thread-safe
- Precision: Standard floating-point precision
- Speed: Slightly slower than integer operations but still very fast