How to remove a certain phrase from a string?

Currently, im making a new feature for admin that i made, in which you can run multiple commands at once. However, i don’t really know how to use strings, i’ve tried string.sub to remove the first part of message which is separated by a " ", but when i try putting another command in, the game just crashes, because it runs the command infinitely because it still thinks that the other part isn’t nil.

Heres a few notes:
cmds = a lowercase split version of the message, which is split by a " ".
msg = the lowercase message
command(plr, msg) = a function that checks if the phrase is a command and if it is, runs the appropriate command, example: “!sit”

if #cmds >= 2 and cmds[2] ~= nil and cmds[2] ~= "" then
	command(plr, string.sub(msg, string.len(cmds[1]) + 1, string.len(msg)))
end
2 Likes

What phrase is it you’re trying to remove?

Any phrase but heres an example:

“!kill all !explode all”

Remove the !kill, which is the first word, then run the command function with the cut message.

So are you asking to seperate the prefix and the command like “!kill” to “kill”

Remove both kill and the prefix. Then keep the rest of the message.

--// this would remove the first word of the string

local String = "!kill all"

print(string.gsub(String, string.split(String," ")[1], "")) 

-- Output: all 1

Why did it print out it with it a “1”?

I’m not really sure, but it wouldn’t matter since it won’t effect any scripting
image
here’s an example of me setting the now split string to a part, doesn’t include the 1.

I tried replacing it into my command function, but it still crashed the game, im not really sure why.

how are you’re commands formatted, with a loop?

Its a really long function that checks if the phrase is one of many commands, it includes both the player who messaged it, and the message. Then in the end, it checks if the message is indeed longer than 1 word, and fires the command function again, but with the first word of the message, cut.

I would find what part causes the uncooled loop in you’re script and either add a wait() cooldown, or make it so it doesn’t check infinite times.

Okay, i added a cooldown, and it seems to think that the next word after “all” isn’t nil.

What word is present after “all” in you’re script

Its just “!kill all”. Nothing after “all”.

It’s not like I can view you’re code and you don’t have to, so I would recommend checking out the code snippets present in these if need to see if you’re not doing something correctly, or just need general inspiration:

The initial question was “Remove A Phrase” and If you have anymore issues with that I would reccomend checking out the documentation for string | Documentation - Roblox Creator Hub

That’s because string.gsub also returns the amount of substitutions it made

@Blacklightsy If I understood what you want correctly, then I think this is essentially what you need to do:

local text = "!kill all"

if string.find(text, "!") == 1 then -- Checks to see if the text starts with !
	local text = string.split(text, " ") -- Splits the text for every space
	local cmd = string.gsub(text[1], "!", "", 1) -- Removes the ! from the command

	print(cmd) -- The command
	print(text[2]) -- The argument
end

Hold on, i think i may have made it work.

Well, kinda, if you type something like “!sit !ff”, it will only do the second command, but i think thats a issue with my code so i might fix that.

Make sure that if you’re using string.find to check that the ! is found at the beginning by using == 1 like in my example