Im trying to make admin commands and say if I want to get the players message and it was
if message == "/large NubblyFry NumberValue"
How would I find out the number value that the player put?
Im trying to make admin commands and say if I want to get the players message and it was
if message == "/large NubblyFry NumberValue"
How would I find out the number value that the player put?
You can use string.split. It returns an array of elements depending on the separator you placed as the argument.
local str = "Hello World"
local separator = " "
local array = string.split(str, separator) -- {"Hello", "World"}
PS:
All whitespace might not be excluded so youād have to make a filter for that or use string.gsub (or even empty slices showed below in the image).
This is a bit confusing to me, can you explain this to me in a TL;DR or make a example?
Sure!
So with string.split, all it does it take the contents of the string, finds out if something called a āseparatorā is found and excludes that from the string. It also appends (or inserts) anything before that āseparatorā into an array. Hereās an example:
local str = "Hello|World"
local separator = "|" -- a "separator" can be anything, as long as it marks the values you want to "separate"
local array = string.split(str, separator) -- remember the explanation before? All this does is take the "separator" and look for it in the string "Hello|World". It finds the separator after "Hello" so, it takes anything before that separator and adds it into an array, then it looks for anything after the separator and adds that also into the array. It repeats the process also until it's at the end of the string.
print(array) -- this will print out TABLE: {"Hello", "World"}
Iāll show an example with Luau in VS Code:
I tried my best to explain, if thereās anything wrong with this explanation, please tell me and Iāll gladly correct it.
AH YES, I kinda forgot
TL;DR:
It just takes a string and separator and removes the separator from the string, then inserts anything before or after the separator into an array.
txt = "/large NubblyFry Numbervalue"
arguments = string.split(txt, " ") --returns an array of the text split by a space
print(arguments[1], arguments[2], arguments[3]) --"/large", "NubblyFry", "NumberValue"
Here is a function I created to split it up for you.
function getArgsFromCommand(commandString)
local SPLIT_STRING = " "
local split = string.split(commandString,SPLIT_STRING)
local command = split[1]
if command then
local prefix = string.sub(command,1,1)
local pureCommand = string.sub(command,2,#command)
return prefix, pureCommand, split[2], split[3]
else
return {}
end
end
local prefixUsed, commandUsed, arg1, arg2 = getArgsFromCommand(CommandStringHere)
I donāt want to litterly make it number value, but for the player to set a number value
local value
if tonumber(arguments[3])
value = tonumber(arguments[3])
else
--some error here ig
end
Please dont just give people scripts without explaining what they actually do and how they even work : /
Im a bit confused, should this be the same part from the other script you gave me? whats the local value for?
Itās the value of the number in the command
I thought you were explained already, and actually spent a few minutes making that just to get a ā:/ā what a shame.
string.split turns a string to a table that each index has been āsplitā by the separator.
string.match(message, "[0-9]+$")
Not sure why you didnāt just get a straight answer but here you go. This will match any number at the end of the string value stored inside the variable named āmessageā.
Hello everyone, so I codded a little function for you with comments:
local function SplitMyString(str)
local results = {} --Gotta store all the words I found here
local current = "" --Our currect word, we are going to collect it letter by letter
for i=1,#str do --Loop from the start of our string to the end
local letter = string.sub(str,i,i) --Letter, or we cut our string from i to i (e.x i=1, from 1 to 1 "I")
if letter == " " then --if our letter is space then, we gotta add our current (word) in table of results
table.insert(results, current) --adding
current = "" --resetting current word
else --else, we add our letter to the current
current = current..letter --yeah
end
end
if #current > 0 then --if we still have current, but our loop ended, we gotta add it out of loop
table.insert(results, current) --adding
end
return results --returns table of words
end
local results = SplitMyString("I like trains")
for i,v in pairs(results) do
print(v)
end
This works but, what does string.match for?