H
H
hoojpop2019-08-11 20:00:16
Lua
hoojpop, 2019-08-11 20:00:16

Script error: bad argument #2 to 'format' (number expected, got string) stack traceback: [C]: in function 'format'?

An error occurs when requesting a function:

function SampEvents.onServerMessage(color, msg)
        if msg:find('Вы можете взять %d+ материалов.') and not sampIsChatInputActive() then
            sampSendChat(string.format('/get guns %d', msg))
        end
    end

Mistake:
[ML] (error) main.lua: D:\Games\SAMP\moonloader\main.lua:34: bad argument #2 to 'format' (number expected, got string)
    stack traceback:
        [C]: in function 'format'
        D:\Games\SAMP\moonloader\main.lua:34: in function 'callback'
        D:\Games\SAMP\moonloader\lib\samp\events\core.lua:80: in function <D:\Games\SAMP\moonloader\lib\samp\events\core.lua:54>
    [ML] (error) main.lua: Script died due to error. (1E3671AC)

What is wrong, please tell me.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dollar, 2019-08-11
@hoojpop

sampSendChat(string.format('/get guns %d', tonumber(msg)))

And even easier:
sampSendChat('/get guns ' .. msg))

Q
q2zoff, 2019-08-11
@q27off

The argument to the string.format function is a string, not a number. Change the modifier in the format specifier from %d to %s.

A
Alexander, 2019-08-11
@zkelo

When using an argument %d, the function string.format()expects you to pass it a number as the second argument. But, judging by the description of the error, you are passing the string
Try this
sampSendChat(string.format('/get guns %s', msg))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question