From cd11095ed2cc817dcd025fa92c277ef416315b2c Mon Sep 17 00:00:00 2001 From: Nordi98 Date: Sun, 20 Jul 2025 22:33:33 +0200 Subject: [PATCH] Update main.lua --- .../[crime]/r_moneywash/src/client/main.lua | 126 ++++++++++++++---- 1 file changed, 98 insertions(+), 28 deletions(-) diff --git a/resources/[jobs]/[crime]/r_moneywash/src/client/main.lua b/resources/[jobs]/[crime]/r_moneywash/src/client/main.lua index 5b0d944e1..47caf8359 100644 --- a/resources/[jobs]/[crime]/r_moneywash/src/client/main.lua +++ b/resources/[jobs]/[crime]/r_moneywash/src/client/main.lua @@ -1,5 +1,21 @@ local entities = {} +-- Enhanced debug function +function _debug(...) + if Cfg.Debug then + local args = {...} + local message = "" + for i, v in ipairs(args) do + if type(v) == "table" then + message = message .. " " .. json.encode(v) + else + message = message .. " " .. tostring(v) + end + end + print(message) + end +end + local function taskNpcGiveEnvelope() Core.Natives.PlayAnim(entities.npc, 'mp_common', 'givetake1_a', 1000, 0, 0.0) Core.Natives.PlayAnim(cache.ped, 'mp_common', 'givetake1_a', 1000, 0, 0.0) @@ -52,12 +68,22 @@ lib.callback.register('r_moneywash:startWashingProgressBar', function() end) local function taskGiveNpcMoney(amount, metadata) + -- Check if amount is valid + if not amount or type(amount) ~= "number" then + _debug('[DEBUG] - Error: Invalid amount in taskGiveNpcMoney') + return Core.Framework.Notify(_L('invalid_amount'), 'error') + end + local cashProp = 'prop_anim_cash_pile_02' entities.cash = Core.Natives.CreateProp(cashProp, Cfg.Options.Location, 0.0, false) AttachEntityToEntity(entities.cash, cache.ped, 90, 0.003, 0.008, 0.015, 44.108, 29.315, 20.733, true, true, false, true, 2, true) Core.Natives.PlayAnim(cache.ped, 'mp_common', 'givetake1_a', 1000, 0, 0.0) Core.Natives.PlayAnim(entities.npc, 'mp_common', 'givetake1_a', 1000, 0, 0.0) - TriggerServerEvent('r_moneywash:startWashingMoney', cache.serverId, amount, metadata) + + -- Ensure metadata is properly handled + local safeMetadata = metadata or {} + TriggerServerEvent('r_moneywash:startWashingMoney', cache.serverId, amount, safeMetadata) + _debug('[DEBUG] - Money given, starting exchange') SetTimeout(750, function() AttachEntityToEntity(entities.cash, entities.npc, GetPedBoneIndex(entities.npc, 28422), 0, 0, 0, 168.93, -83.80, 76.29, true, true, false, true, 2, true) @@ -66,7 +92,25 @@ end local function giveExchangeOffer(amount, metadata) local taxRate = lib.callback.await('r_moneywash:getTaxRate', false) - local given = amount if metadata then given = metadata.worth end + + -- Check if amount is nil + if not amount then + _debug('[DEBUG] - Error: amount is nil in giveExchangeOffer') + return Core.Framework.Notify(_L('invalid_amount'), 'error') + end + + -- Set given with proper checks + local given = amount + if metadata and metadata.worth then + given = metadata.worth + end + + -- Double check that given is a valid number + if not given or type(given) ~= "number" then + _debug('[DEBUG] - Error: given is not a valid number in giveExchangeOffer') + return Core.Framework.Notify(_L('invalid_amount'), 'error') + end + local offer = math.ceil(given - (given * taxRate / 100)) local confirm = lib.alertDialog({ header = _L('money_wash'), @@ -84,28 +128,43 @@ local function buildMarkedBillsMenu() ClearPedTasks(entities.npc) PlayPedAmbientSpeechNative(entities.npc, 'GENERIC_HOWS_IT_GOING', 'SPEECH_PARAMS_FORCE') local playerInventory = lib.callback.await('r_moneywash:getPlayerInventory', false) -for _, item in pairs(playerInventory) do - if item.name == Cfg.Options.Currency then - -- Add a check for metadata - local description = "" - if item.metadata and item.metadata.worth then - description = _L('marked_worth', item.metadata.worth) - else - description = _L('marked_worth', item.count) -- Fallback to using the count or another appropriate value - end - - table.insert(options, { - title = item.label, - description = description, - icon = 'fas fa-money-bill-wave', - iconColor = '#fa5252', - onSelect = function() - giveExchangeOffer(item.count, item.metadata) - end, - }) + + if not playerInventory or type(playerInventory) ~= "table" then + _debug('[DEBUG] - Error: Invalid player inventory') + return Core.Framework.Notify(_L('inventory_error'), 'error') end -end - + + for _, item in pairs(playerInventory) do + if item and item.name == Cfg.Options.Currency then + -- Add a check for metadata and item count + local description = "" + if item.metadata and item.metadata.worth then + description = _L('marked_worth', item.metadata.worth) + else + description = _L('marked_worth', item.count or 0) -- Fallback to using the count or 0 + end + + table.insert(options, { + title = item.label or Cfg.Options.Currency, + description = description, + icon = 'fas fa-money-bill-wave', + iconColor = '#fa5252', + onSelect = function() + -- Check if item.count exists and is a number + if not item.count or type(item.count) ~= "number" then + return Core.Framework.Notify(_L('invalid_amount'), 'error') + end + giveExchangeOffer(item.count, item.metadata) + end, + }) + end + end + + if #options == 0 then + _debug('[DEBUG] - No marked bills found in inventory') + return Core.Framework.Notify(_L('no_marked_bills'), 'error') + end + lib.registerContext({ id = 'moneywash_markedbills', title = _L('money_wash'), @@ -117,13 +176,24 @@ end local function openMoneyWashInput() local playerCash = lib.callback.await('r_moneywash:getInventoryItem', false, Cfg.Options.Currency) - if playerCash.count < Cfg.Options.MinWash then return Core.Framework.Notify(_L('not_enough_money', Cfg.Options.MinWash), 'error') end - if playerCash.count > Cfg.Options.MaxWash then playerCash.count = Cfg.Options.MaxWash end + if not playerCash or not playerCash.count then + return Core.Framework.Notify(_L('not_enough_money', Cfg.Options.MinWash), 'error') + end + + if playerCash.count < Cfg.Options.MinWash then + return Core.Framework.Notify(_L('not_enough_money', Cfg.Options.MinWash), 'error') + end + + if playerCash.count > Cfg.Options.MaxWash then + playerCash.count = Cfg.Options.MaxWash + end + PlayPedAmbientSpeechNative(entities.npc, 'GENERIC_HOWS_IT_GOING', 'SPEECH_PARAMS_FORCE') local input = lib.inputDialog(_L('wash_money'), { { type = 'number', label = _L('wash_amount'), icon = 'dollar-sign', required = true, min = Cfg.Options.MinWash, max = playerCash.count }, }) - if not input then return end + + if not input or not input[1] then return end giveExchangeOffer(tonumber(input[1])) end @@ -210,7 +280,7 @@ function locPoint:onEnter() distance = 2, onSelect = function() local onCooldown = lib.callback.await('r_moneywash:getPlayerCooldown', false) - print(onCooldown) + _debug('[DEBUG] - Player cooldown status:', onCooldown) if onCooldown then Core.Framework.Notify(_L('on_cooldown'), 'info') return end if Cfg.Options.Currency == 'markedbills' then return buildMarkedBillsMenu() end openMoneyWashInput() @@ -240,4 +310,4 @@ AddEventHandler('onResourceStop', function(resource) if DoesEntityExist(entity) then DeleteEntity(entity) end end end -end) \ No newline at end of file +end)