What you should be using is string.match (or string.find). You would also want to use string patterns, to allow a different value to be returned.
string.match("Hello","Hell")
string.match("Yes","No")
Here is some complicated code that essentially forms the backbone of any admin script:
local prefix = ":"
local valType = {}
valType.player = "player"
valType.number = "number"
local commands = {
speed = {
values = {valType.player,valType.number},
func = function(...)
local args = {...}
local player = args[1]
if player and player:IsA("Player") then
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")
args[2] = tonumber(args[2])
if args[2] then
humanoid.Walkspeed = args[2]
end
end
end
}
}
function getArgs(msg)
local args = {}
while msg ~= "" or string.match(msg," ") do
local str = string.match(msg,"(%.)%n+") or msg
table.insert(args,str)
local strEnd = string.match(msg,"(%n+)") or ""
msg = string.gsub(msg,str..strEnd,"")
end
return args
end
function decodeMessage(msg)
local pref = string.sub(msg,1,string.len(prefix))
if pref == prefix then
msg = string.gsub(msg,prefix,"")
local args = getArgs(msg)
local cmd = cmds[args1]
if cmd then
table.remove(args,1)
args = assignValues(args,cmd.values)
cmd.func(unpack(args))
end
end
end
[1]
This line finds the prefix, using the length of the prefix as a guide - string.match may be unreliable because someone may use ':' later in the script.
[2]
string.gsub essentially replaces one part of a string with another string. This returns the new string, so it does not automatically update variables.
string.gsub("Yes","Yes","No")
[3]
String patterns:
() = Captures a pattern, returning what is captured. Multiple captures in one string returns a tuple.
%n = Whitespace character.
%. = Any character.
+ (suffix) = Gets as many characters as possible.
[4]
I won't write this function, but it essentially assigns each argument the corresponding argument defined in the 'values' array in a command's dictionary.
For example, for the speed command, it makes the first argument a player, and the second argument a number. I'm using 'valType' cause it looks cool (lol), and just organises everything better.
I understand this is very very very very very very complicated for beginners, so this will probably look very daunting (especially in the SH code block).
If you need some more help understanding parts of it, please ask.
Also note that this code is theoretical, so it may not work (I probably have made some typos, too)
Hope I helped!
~TDP
1 comment
I've completed my answer.