Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to use string.sub?

Asked by 9 years ago

I am trying to make a prefix to this, but I do not know how..

How would I make a Prefix to the followng?

game.Players.ChildAdded:connect(function(Player)
    if Player:IsA("Player") then
        if Player.Name == "Player" then
            Player.Chatted:connect(function(Test)

            end)
        end
    end
end)

I really need help on this, and it would be appreciated if helped.

0
What do you mean by prefix? YellowoTide 1992 — 9y
0
What Do You Mean? ~Justin Bieber Vezious 310 — 9y
0
Like :dothis ReturnValue 50 — 9y
0
string.find(":") and then check to make sure it's the first thing said, and then do string.sub() for everything after the ":". Legojoker 345 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

Easy,

if Test:sub(1,1) == ":" then -- If the first character is a :
    Test=Test:sub(2) -- Change the message to the letter after the prefix
    if Test=="a" then -- If you said :a
        print'a' -- print "a" to the console
    end -- Ends the if Test=="a"
end -- Ends the if Test:sub(1,1) == ":"

If you have any problems, tell me, if you want a more efficient method though:

local Commands={}
AddCommand=function(name,desc,usage,func)
    Commands[name]={Note=desc,Say=usage,Function=func}
end
Chatted=function(p)
p.Chatted:connect(function(m)
    table.foreach(Commands, function(i,v)
        if m:sub(1,1+#v.Say) == ":"..v.Say then
            v.Function(p,m;sub(2+#v.Say)
        end
    end)
end)
end
game:service'Players'.PlayerAdded:connect(Chatted)

-- EXAMPLE FOR ADDCOMMAND (This outputs all commands in an orange/yellow font)
AddCommand("Commands","Gets all cmds", "cmds", function(p)
    print(p.Name)
    for i,v in next, Commands do
        warn(i,v.Note,v.Say,"F"..tostring(v.Function:sub(2)))
    end
end)

These aren't tested, just an FYI

Ad

Answer this question