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 string.split() though.
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
print(msg)
local Reason = msg:sub(string.len("!gshutdown"))
print(Reason)
GlobalShutdown.Main(Reason, BlacklistedUsers.GlobalShutdown, plr)
end
-- rest of the code....
You’d better use the string.match function.
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."
end
print(Reason) -- outputs "Game Test"
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.