How to remove a certain phrase from a string?

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

I fixed it, i can’t really explain how however.

1 Like

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