Is it possible to use anonymous functions inside of string functions?

As the title says, is something like this possible or am i just doing it wrong?

print(string.format("Redeemed Code: \"%s\"\nPlayer: %s", code.Value, function()
    if plr.DisplayName == plr.Name then
        return plr.Name
    else
        return string.format("%s [%s]", plr.Name, plr.DisplayName)
    end
end))
1 Like

You totally can. Just call the function so the expression in the string.format parameter list evaluates to whatever the function returns. If you want to call a function literal, it must be anonymous and be wrapped in parentheses.

E.g. this doesn’t work

function()
    return "hi"
end()

But this does

(function()
    return "hi"
end)()

If you don’t call the function, Lua’s tostring does its best and gives you something like function: 0x13128b0

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.