You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? So I am working on an admin commands project that has multiple commands an admin can use. One of the commands I am having problems with is
!gshutdown, which is global shutdown that shuts down all servers of the game. I want the admin to also be able to provide a reason for why the shutdown happened, and if there is no reason, it would return “No reason provided.”.
What is the issue? When the admin doesn’t enter a reason, the reason variable will be just be “n”. If the admin inputs a space, the reason variable will just be a space. I’m just wondering if there’s any alternative method I should use. I will not use
What solutions have you tried so far? Yeah, so I tried using
string.split() the very first time and obviously, it separated all the words. Then I began trying to use
string.sub() but now I’m having this problem.
For those who thing I should do this:
local Reason = msg:sub(string.len("!gshutdown")) or "No reason provided."
No, it does not work.
-- rest of the code....
elseif string.match(msg:lower(), "!gshutdown") and table.find(Admins, plr.UserId) then
if EnabledCommands.GlobalShutdown then
local Reason = msg:sub(string.len("!gshutdown"))
GlobalShutdown.Main(Reason, BlacklistedUsers.GlobalShutdown, plr)
-- rest of the code....
I’m not completely sure but I think string.sub is including the n at the end of !gshutdown, So you would have to do a +1 to the result of string.len.
Yes, I tried that as well, but then it would still return a space. And I think the same thing, that is indeed the case.
You’d better use the
Here is an example of implementation:
local cmd = "!gshutdown Game Test"
local pattern = "!gshutdown%s+(.+)" -- matches "!gshutdown" and any number of spaces (one or more). Also, captures the reason argument.
local Reason = string.match(cmd, pattern)
if Reason == nil or string.match(Reason, "^%s+$") then -- if the reason is all spaces or nil (not provided)
Reason = "No reason provided."
print(Reason) -- outputs "Game Test"
Alright, I’ll try it tomorrow.
Thanks a lot! It works very well. Did you do this all by yourself or look at docs? String patters seem very useful and I’d like to learn them.
This actually was a simple pattern, there are complicated ones other than this.
Let’s say, I have to look at the documentation if I’m not sure about the pattern I’m writing, along with trying it on a web-based compiler to check if it works.
You will surely look on the Roblox documentationString Patterns or Lua org documentation for patterns.
So here I assume you used a space as your separator for the
string.split(), if you, however, used the command (
!gshutdown ) followed by a space, then the second index would be the reason!
Just adding information here, I am aware it has been solved.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.