[SOLVED] attempt to index a number value (local 'args')

I’m trying to create a channel marker or alert system (I’m gonna figure that out when I can make arguments function) with pma-voice and scully radio. Everything works as expected but I want to send different sounds by making it get args and if its equal to 1 it will send the sound. it will send another variant if its 2 etc…
The issue is, the arg is always equal to -1. I’ve tried it with
local arg = args
or
local arg = args[1] too. If it’s args[1], the error is attempt to index a number value (local ‘args’)

Code:

local QBCore = exports['qb-core']:GetCoreObject()

RegisterCommand("alert", function(source, args, rawCommand)
    if source == 0 then
        -- Command is being executed from the console
        TriggerClientEvent('playChannelMarkerSound', -1, args, rawCommand)
        print('Marker successfully executed.')
        Citizen.Wait(5000)
        TriggerClientEvent('playChannelMarkerSound', -1, args, rawCommand)
        print('Marker successfully executed.')
    else
        local player = source
        if QBCore.Functions.HasPermission(player, 'god') or QBCore.Functions.HasPermission(player, 'admin') or QBCore.Functions.HasPermission(player, 'mod') or IsPlayerAceAllowed(player, 'command') then
            TriggerEvent('playChannelMarkerSound', -1, args, rawCommand)
            Citizen.Wait(5000)
            TriggerEvent('playChannelMarkerSound', -1, args, rawCommand)
        else
            TriggerClientEvent('okokNotify:Alert', source, "Error", "You're not an admin.", 5000, 'error')
        end
    end
end)


RegisterServerEvent('playChannelMarkerSound')
AddEventHandler('playChannelMarkerSound', function(args)
    local soundName = Scully.MarkerSoundName
    local volume = Scully.MarkerSoundVolume
    local arg = args[1]
    for _, channel in pairs({1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5}) do
        local players = exports['pma-voice']:getPlayersInRadioChannel(channel)
        for source, isTalking in pairs(players) do
            if arg == 1 then
                print(arg)
                TriggerClientEvent('InteractSound_CL:PlayOnOne', source, soundName, volume)
            else
                print(arg)
                TriggerClientEvent('okokNotify:Alert', source, "Error", "Wrong channel.", 5000, 'error')
            end
        end
    end
end)

Any help is appreciated.
(ignore the soundName since I will remove it when args start to work, or use the command as /alert . For now I’m just trying to make it work like /alert )

Not sure why this happens, but I would give assigning args to a local variable before triggering the event a shot, just like you did with the player source. I had similar weird things happen with player sources and this always fixed it for me.

local arguments = args -- Inside the register command function

Didn’t fix it unfortunately.
What I don’t understand is, why -1?
It’s not nil, so it’s getting something which is -1, WHY? I’m about to go mad lol

I think I found the issue. In your TriggerEvent call you are passing -1 as a parameter which is not necessary for server side events. That means all your parameters have been shifted by one index.

I guess I had to hear it from someone, I removed it and now it works. Other than that, the only thing that I had to do was changing the
if arg == 1 then
to:
if arg == “1” then
The reason I had the ‘-1’ there was to trigger the event on every single client because earlier it was a ‘TriggerClientEvent’ , and I used a client script with an event called ‘usemarker’ to send the args and trigger the ‘playChannelMarkerSound’ , which then I realized was not a good idea and complicated things lol. It was quite hard to figure out how to send an event to every single client. I forgot to remove the -1 which makes me feel even more stupid :skull: :skull:
Thanks a LOT

1 Like