[HELP] Issue converting a command value to number

Hi all, Im trying to make a basic command for my addon all it does is trigger my PayDeposit Event to remove the players money acoording to the amount they input aswell as to add that amount to the CashPool to then later be paid out to the staff.

Client Side:

RegisterCommand("mcpay", function(amount)
    local amt = tonumber(amount)
    TriggerServerEvent("dgrp_mcdonalds:PayDeposit", amt)
    TriggerServerEvent("dgrp_mcdonalds:addToCashPool", amt)
    dPrint("You paid $"..amt.." to McDonalds")
    --Trigger Server event to Notify McDonalds Staff that this Player has paid
end)

Server Side:

RegisterServerEvent('dgrp_mcdonalds:payDeposit')
    AddEventHandler('dgrp_mcdonalds:payDeposit', function(amount)
	local xPlayer = ESX.GetPlayerFromId(source)	
	xPlayer.removeMoney(math.floor(amount))	
end)

local cashPool = 0

RegisterServerEvent('dgrp_mcdonalds:addToCashPool')
	AddEventHandler('dgrp_mcdonalds:addToCashPool', function(amount)
	cashPool = cashPool + amount
	print("Added $"..amount.." to CashPool - total $"..cashPool)
end)

Screenshots:


Screenshot_22
After typing to /mcpay 500, this also did not remove any money from my player account, however the PayDeposit is used for many other functions in my addon that take money from the player and works successfully, but just not for an amount that has been typed in the command.

RegisterCommand has three arguments: source, args and rawCommand.

You are trying to get command arguments from the source; what you want to do is this:

RegisterCommand("mcpay", function(_, args)
    if args[1] then
        local amt = tonumber(args[1])

        if amt then
            TriggerServerEvent("dgrp_mcdonalds:PayDeposit", amt)
            TriggerServerEvent("dgrp_mcdonalds:addToCashPool", amt)
            dPrint("You paid $"..amt.." to McDonalds")
            --Trigger Server event to Notify McDonalds Staff that this Player has paid
        else
            dPrint(amt .. " is not a valid amount.")
        end
    else
        dPrint("No amount provided.")
    end
end)

ahh I see, so the ‘_’ is acting as the source in the function and the args is the value that has been input?
And even if the source is not required for this specific function, it is still nesecary to have it in the ‘first slot’ of the commands function?
also what does the ‘[1]’ represent? when using the tonumber() function? Is this asking for its current format perse? ie [1] = string and tonumber converts [1] (string) to number?

That you for pointing out the error, if you could explain to me the questions I have above that would be awesome and further help me understand what is actually happening. Kind Regards.

_ basically means ignore, since we don’t need it, we don’t need to define it as source. Args is a list of all the arguments provided.

Yes, but since you don’t need it you can use _.

The first argument in the list of arguments.

Let me know if any of that doesn’t make sense.

1 Like

That makes perfect sense now, thank you so much and thank you for taking the time to explain it for me. :slight_smile::+1:

1 Like