Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

string substitution returning unwanted value?

i am working on command formatting right now, and for my code i have implemented this solution:

-- context
local Message = ".lock true"
local SL_SETTINGS = {CommandPrefix = "."}

-- actual script
local splitMessage = Message:split(" ")
local command = splitMessage[1]
local no_prefix = string.gsub(command, SL_SETTINGS.CommandPrefix, "")

print(command, no_prefix)
--> ".lock" ""

the issue i am experiencing is that line 8 is producing a substituted string that i don't want, i get an empty string when i expect "lock"

does anybody know why this is happening and how i could fix this code?

1 answer

✓ Accepted answer

. is a string pattern that represents any character; therefore, it is replacing all the characters in the string.

You can use the %p string pattern, which removes all punctuation from the string, or you can split the prefix from the string.

local Message = ".lock true"
local SL_SETTINGS = {CommandPrefix = "."}

local splitMessage = Message:split(" ")
local command = splitMessage[1]
local no_prefix = command:gsub("%p", "") -- or command:split(SL_SETTINGS.CommandPrefix)[2]

print(command, no_prefix)
--> ".lock" "lock"

1 comment

0

oh my god, yes. i completely forgot about literals. thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.