Team change command not working

I want to achieve a command that allows you to change the team but you’ll get an error when your inside the team you want to change to, i also want the command to work like in javascript with: toLowerCase(), the issues is that is simply doesn’t work, it doesn’t even throw errors, just does nothing at all, i’ve tried several ways of trying to fix this but i can’t find any solution

The script:

local Teams = {
	ST = BrickColor.new("Deep orange"),
	C = BrickColor.new("Forest green")
}
local NotificationEvent = game.ReplicatedStorage.Events:WaitForChild("NotificationEvent")
local ErrorMessage = "You can't change to that team while your in that team"
local command = "changeteam"
local prefix = "!"

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		if msg:sub(1,prefix:len()) == prefix then
			if msg:sub(1,command:len()):lower() == command then
				if msg:sub(13):lower() == "st" or "sewer" or "sewerteam" then
					if Player.TeamColor == Teams.ST then
						NotificationEvent:FireClient(Player,ErrorMessage)
					elseif not Player.TeamColor == Teams.ST then
						Player.TeamColor = Teams.ST
					end
				elseif msg:sub(13):lower() == "c" or "civilian" or "civilianteam" then
					if Player.TeamColor == Teams.C then
						NotificationEvent:FireClient(Player,ErrorMessage)
					elseif not Player.TeamColor == Teams.C then
						Player.TeamColor = Teams.C
					end
				end
			end
		end
	end)
end)

Have you tried putting print statements before/after each connection and if statement to work out where it is unexpectedly not progressing. Alternatively have you tried using the debugger to step through and see where it stops?

I already tried that, i just removed these steps in that script

So at which point was it not progressing where it should?

It only progressed through the Player.Chatted event but didn’t came through the command itself

You’re accounting for the first character in the command which is the prefix, but you’re checking for the command name without the prefix hence why it doesn’t go through. You’d want to start from the 2nd character:

if msg:sub(2, command:len()):lower() == command then

Generally, if your prefix isn’t a single character:

if msg:sub(#prefix+1,command:len()):lower() == command then

So i changed it to this now:

local Teams = {
	ST = BrickColor.new("Deep orange"),
	C = BrickColor.new("Forest green")
}
local NotificationEvent = game.ReplicatedStorage.Events:WaitForChild("NotificationEvent")
local ErrorMessage = "You can't change to that team while your in that team"
local command = "changeteam"
local prefix = "!"

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		warn("1")
		if msg:sub(#prefix+1,command:len()):lower() == command then
			warn("2")
			if msg:sub(13):lower() == "st" or "sewer" or "sewerteam" then
				warn("3")
				if Player.TeamColor == Teams.ST then
					warn("4")
					NotificationEvent:FireClient(Player,ErrorMessage)
				elseif not Player.TeamColor == Teams.ST then
					warn("4.5")
					Player.TeamColor = Teams.ST
				end
			elseif msg:sub(13):lower() == "c" or "civilian" or "civilianteam" then
				warn("5")
				if Player.TeamColor == Teams.C then
					warn("6")
					NotificationEvent:FireClient(Player,ErrorMessage)
				elseif not Player.TeamColor == Teams.C then
					warn("6.5")
					Player.TeamColor = Teams.C
				end
			end
		end
	end)
end)

But it still doesn’t get past 1

Are you testing this in studio? The player joins before scripts start, so that might be the issue

I tried this out on a game it still comes with the same result of not getting past 1

Remember, when adding the prefix length + 1, you also need to add that to the command’s length.

Try this:

if msg:sub(#prefix + 1, command:len() + #prefix + 1):lower() == command then

Even with this, it doesn’t get past 1

Alright then, I’m not sure if #prefix works, try replacing the #prefix’s with prefix:len().


It still doesn’t get past 1 (Don’t mind the 14, i just added warn(#msg) to that Player.Chatted Event)

May I see your current code, please?

This is the current state:

local Teams = {
	ST = BrickColor.new("Deep orange"),
	C = BrickColor.new("Forest green")
}
local NotificationEvent = game.ReplicatedStorage.Events:WaitForChild("NotificationEvent")
local ErrorMessage = "You can't change to that team while your in that team"
local command = "changeteam"
local prefix = "!"

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		warn("1")
		warn(#msg)
		if msg:sub(prefix:len()+1,command:len()+prefix:len()+1):lower() == command then
			warn("2")
			if msg:sub(13):lower() == "st" or "sewer" or "sewerteam" then
				warn("3")
				if Player.TeamColor == Teams.ST then
					warn("4")
					NotificationEvent:FireClient(Player,ErrorMessage)
				elseif not Player.TeamColor == Teams.ST then
					warn("4.5")
					Player.TeamColor = Teams.ST
				end
			elseif msg:sub(13):lower() == "c" or "civilian" or "civilianteam" then
				warn("5")
				if Player.TeamColor == Teams.C then
					warn("6")
					NotificationEvent:FireClient(Player,ErrorMessage)
				elseif not Player.TeamColor == Teams.C then
					warn("6.5")
					Player.TeamColor = Teams.C
				end
			end
		end
	end)
end)

And you’re typing !changeteam in chat, correct?

Yes i do, like i’ve said i’m trying to get this to work like in javascript with .toLowerCase()
That’s why i’m typing into the chat: !ChangeTeam st
But i also tried just typing it in only lowercase

This is very odd. I have tried your code in my own game and it seems to work just fine.

Screen Shot 2020-07-07 at 10.11.55 AM

EDIT: It doesn’t seem to work when you say something after the !changeteam command.

Alright, it seems to work fine for me now, try this code:

local Teams = {
	ST = BrickColor.new("Deep orange"),
	C = BrickColor.new("Forest green")
}
local NotificationEvent = game.ReplicatedStorage.Events:WaitForChild("NotificationEvent")
local ErrorMessage = "You can't change to that team while your in that team"
local command = "changeteam"
local prefix = "!"

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		warn("1")
		if msg:sub(prefix:len()+1,command:len()+prefix:len()):lower() == command then
			warn("2")
			if msg:sub(13):lower() == "st" or "sewer" or "sewerteam" then
				warn("3")
				if Player.TeamColor == Teams.ST then
					warn("4")
					NotificationEvent:FireClient(Player,ErrorMessage)
				elseif not Player.TeamColor == Teams.ST then
					warn("4.5")
					Player.TeamColor = Teams.ST
				end
			elseif msg:sub(13):lower() == "c" or "civilian" or "civilianteam" then
				warn("5")
				if Player.TeamColor == Teams.C then
					warn("6")
					NotificationEvent:FireClient(Player,ErrorMessage)
				elseif not Player.TeamColor == Teams.C then
					warn("6.5")
					Player.TeamColor = Teams.C
				end
			end
		end
	end)
end)


Yeah i don’t get why it gets after 3 even though I didn’t tell st or something else