How do you get specific parts of a string?

i’m kind of new with working with “more advanced strings”, and i need help with getting multiple specific parts of a string

illustrated example of what i mean sort of:

red would be who i’d want to ban,
green would be the ban duration (-1 being permanent),
and blue being the reason for punishment.

split the string by spaces, assume the first part is the command, enter a specific parsing process for that specific command(in this case the ban command), then parse the next tokens(in this case playerName and -1) and for whatever comes afterwards, assume its text and recombine it into a single token(using the reverse of split, which can be done using table.concat(messageParts, " ")).

Basically assume that the command, the player name, and the ban length can’t have spaces in them. Only the ban reason can.

1 Like

Kind of looks like you would build that string not have to separate parts from it.

Anyways…

local str = "!ban SomePlayerName -1 Banned for hacking."
local name = string.match(str, "^!ban%s+(%S+)")
local mode = string.match(str, "^!ban%s+%S+%s+(-?%d+)")
local info = string.match(str, "^!ban%s+%S+%s+%-?%d+%s+(.+)")
print(name)
print(mode)
print(info)

Roblox has so many command for strings. You could do this many ways.

if you forget the prefix “!” (using whatever method you prefer), you have a nice string which you can use string.split(" ") on to turn into an array of useful strings.

If you use table.remove(msg, 1) once on this array, you can retrieve the command name.

Doing table.remove(msg, 1) again gives you the playerName

A third time gives you the duration.

Finally, the remaining parameters are the ban reason and can be combined using table.concat(msg, " ")

Whoops, forgot to reply, but I found my own solution.

if string.sub(message, 1, #"!ban"):lower() == "!ban" then
				--ban format: !ban username duration(seconds) reason
				local args = string.split(message, " ");
				local numbers = "%d" or "%d%d";
				--[[
				for _,c in args do
					print(c)
				end]]
				local duration = tonumber(args[3]);
				local playerToBan = players:GetUserIdFromNameAsync(args[2]);
				local reason = string.sub(message, string.len("!ban " .. args[2] .. " " .. duration) + 1); --search for reason
				local playerwhobanned = player.Name
				--print(duration)
				if not table.find(admins, playerToBan) then
					ban(playerToBan, reason, duration, playerwhobanned)
				else
					error("player:"..player.Name.." cannot ban admins")
				end

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