我正在尝试在Roblox中创建命令参数。例如,/kill playername
。问题是我不知道如何从字符串中解析播放器名称/kill playername
。这段代码是这样的:
game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Message)
if string.sub(1, #Message) == "/kill " then
--this means the string starts with /kill and is expecting an argument.
--How can I parse this argument from the string
end
end)
end)
编辑:我想添加/setdata <Playername> <DataToChange eg. money> <Value>
示例命令:
/setdata MyRobloxUsername Money 10000
我正在尝试使用类似的方法
local Command, Playername, DataToChange, Value = string.match(???)
我只需要将字符串中的值转换成变量即可。我可以自己弄清楚如何使用变量更改数据。只是如何从字符串中获取值。我该怎么做?
我无法接受答案,因为我需要进一步的帮助。获得此帮助后,我将重新接受它。我的下一个请求与此类似,但是使用3个参数而不是1个参数。我需要帮助,因为string:Match()
这对我而言非常直观
如果您希望将来对更多命令更加灵活,我建议您同时考虑lhf和BotOfWar的建议并将其合并。
local function executeCommandInMessage(message)
-- do a quick regex of the message to see if it is formatted as a command
-- all we care about is the command, any arguments are optional.
local command, arguments = string.match(message, "^/(%w+)[%s]?([%w%s]+)$")
if command ~= nil then
-- we've found a command, parse the arguments into groups of non-space characters
-- then store each word in the parts array
local parts = {}
for w in arguments:gmatch("%S+") do
table.insert(parts, w)
end
-- handle each command individually
if command == "kill" then
local player = parts[1]
print(string.format("Killing %s", player))
elseif command == "setdata" then
local player = parts[1]
local value = parts[2]
local amount = parts[3]
print(string.format("Setting %s on %s to %s", value, player, amount))
-- add any further commands to the list..
-- elseif command == "" then
end
end
end
-- listen for any message submitted by players
game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(msg)
-- check for any commands
executeCommandInMessage(msg)
end)
end)
将来,如果您需要更好的正则表达式来解析消息,建议您看一下如何进行Lua模式匹配。一旦您知道要看的内容,它们就很容易阅读。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句