How do I get the first character of a string?

Hi. I am trying to make an admin script. At my current stage, I need to make sure the string starts with the command identifier. I always thought I would just do something like variable[1] but It is not working.

Here is all of my code:

local players = game:GetService("Players")

local admins = {225626108}
local pre = ':'

function isIn(val, tabl)
	for k,v in pairs(tabl) do
		if v == val then
			return true
		end
	end
	return false
end

players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(text)
		print(text, text[1])
		if isIn(player.UserId, admins) == true then
			if text[1] == pre then
				print("command")
			else
				print("not command")
			end
		end
	end)
end)

No matter what I say, I get “not command” in the output. Also, when I print the text which I am inputting, I get the expected text, so that is fine. Also, when I print text[1] I get ‘nil’.

You want to use the string.sub method.

string.sub(text, 1, 1) == pre

8 Likes