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

How to use string.sub?

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.

4 comments

0

What do you mean by prefix?

0

What Do You Mean? ~Justin Bieber

0

Like :dothis

0

string.find(":") and then check to make sure it's the first thing said, and then do string.sub() for everything after the ":".

1 answer

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

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