Lua 在玩家名称中查找 "\x"

我的服务器上的玩家正在使用表情符号加入他们的名称,这是不允许的,我试图创建一个 if 检查,但它不能正常工作。

if string.find(playerName, "%^") or string.find(playerName, '%\') then
    deferrals.done("请删除您的名称中的表情符号和颜色代码。")
end
点赞
用户4984564
用户4984564

语法高亮已经告诉你哪里出错了:\ 在 Lua 中被用作转义字符,所以 \ 后面的 ' 没有关闭字符串,而是直到代码中的下一个 '。如果想要一个单独的 \,请使用 \\

if playerName:find("%^") or playerName:find("%\\") then
   deferrals.done("Por favor, elimine sus emojis y códigos de color de su nombre.")
end

顺便说一下,如果你不需要在搜索中使用模式,你可以通过将 false 作为第二个参数传递给 string.find 来禁用它们:

if playerName:find([[^]], 1, false) or playerName:find([[\]], 1, false) then
   deferrals.done("Por favor, elimine sus emojis y códigos de color de su nombre.")
end

如果可以的话,试着在条件中添加 or true,这样它就会激活所有玩家的名称。这样你就可以找出是条件本身失败了还是 if 块内部的代码失败了。

2019-12-03 07:14:04