Player.Chatted / string.sub

Hello, I’ve been making commands and it’d come to my attention that I can only make two commands and then I’m not able to make any more or if I do they don’t respond with anything, I was looking for a solution to this, would be really helpful. Thank you.

1 Like

Clear Up Confusion

Hello! I read your post 3 times and I still don’t understand what you’re attempting to communicate. Can you please elaborate?

Hopefully Related

If you’re asking how to split your arguments and command that was called I prefer to put my commands in a dictionary where the key of the dictionary is my command, and the value is an anonymous function that I call with my arguments when the command is called.

Splitting Command and Arguments

I just use a simple string.split and table.remove method.

local arguments = input:split(separator)
local command = table.remove(arguments, 1)

Where input is the player’s message, separator is the separator between the arguments (">kill a,b,c,"; , is the separator in this case.)

Command Organization

local commands = {
    ["epic"] = function(caller, args)
        print(caller, args)
    end
}

(I usually add all of my functions via requiring modules for a cleaner workflow, that is just an example code block)

Examples

Here is examples of my studio hierarchy for an old admin I wrote (these are like 12 months old but my style is still similar):
1:
image
2:
image

1 Like

To add onto this, this post can be helpful:

1 Like

To add onto this, this is an example of how I do it…

aaaaaaa

So, I’ve basically got 4 commands. ‘!afk’,‘!unafk’,‘!host’ and ‘!unhost’. Currently while I join the game and I try type one of those commands only two of them work, which are !afk and !unafk. These two are at the start of the script and I’m willing to bet if I put !host and !unhost at the start of the script then these would then switch and !afk and !unafk wouldn’t work, what I’m actually trying to do is get all four of these to work and not just two of them, I don’t know if there’s a restriction on these or something. I’m currently only a beginner programmer so I don’t know if I’d be able to do it your way, I hope this helps clear what I’m trying to do.

Hey! I suggest posting your code so people viewing the thread can view it and post if they see any mistakes. I recommend using the code formatting option by the way, makes it much easier to read the code. Just post your code then select it and click this

image

Also, if you have a hypothesis of what might fix your issue then try it out and report back the results.

1 Like

Another thing to add this is is that they’re all stored in a Core script which is linked to a Rank Tag and all use the rank tag which is why they’re all together in one script.

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local GUI = ReplicatedStorage:FindFirstChild('ReplicatedUI').Rank:Clone()
		GUI.Parent = Character.Head
		GUI.Frame.Username.Text = Player.Name
		if Player:GetRoleInGroup(Module.Configuration.GroupId) == "Guest" then
			GUI.Frame.Rank.Text = "Supporter"
		elseif Player.UserId == 0 then
			GUI.Frame.Rank.Text = "N/A"
		else
			GUI.Frame.Rank.Text = Player:GetRoleInGroup(Module.Configuration.GroupId)
		end
		if Player.UserId == 164877086 then
			GUI.Frame.Premium.Text = "adrian"
		end
		for _,v in pairs(Module.Whitelisted) do
			if Player.Name == v then
				GUI.Frame.Premium.Visible = true
			end
		end
		
		Player.Chatted:connect(function(chat)
			if string.sub(chat, 0, string.len("!afk")) == "!afk" then
				GUI.Frame.Rank.Text = "AFK"
				local forceField = Instance.new("ForceField")
				forceField.Visible = true
				forceField.Parent = Character
				if Player and Player.Character then
				if Player.Character:FindFirstChildWhichIsA("Humanoid") then
				Player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = 0
				print("Player is now AFK")
				
		Player.Chatted:connect(function(chat)
			if string.sub(chat, 0, string.len("!unafk")) == "!unafk" then
				GUI.Frame.Rank.Text = Player:GetRoleInGroup(Module.Configuration.GroupId)
				if Player and Player.Character then
				if Player.Character:FindFirstChildWhichIsA("Humanoid") then
				Player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = 16
				forceField.Visible = false
				forceField.Duration = forceField.Visible
				print("Player is now unAFK")
				
		Player.Chatted:connect(function(chat)	
			if string.sub(chat, 0, string.len("!host")) == "!host" then
				if Player:GetRankInGroup(4805526) >= 242 then
					GUI.Frame.Premium.Visible = true
					GUI.Frame.Premium.Text = "Hosting Team"
					print("Success")
				else
					print("Unsuccessful")
					end
		
			elseif string.sub(chat, 0, string.len("!unhost")) == "!unhost" then
				print("Success")
				if Player:GetRankInGroup(4805526) >= 242 then
					if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, Module.Configuration.GamepassId) == true then
						GUI.Frame.Premium.Visible = true
						print("Success")
						if Player.UserId == 164877086 then
							GUI.Frame.Premium.Text = "adrian"
						else
							GUI.Frame.Premium.Text = "Premium"
						end
					else
						GUI.Frame.Premium.Visible = false
					end 
				end
			end

Here is an example:
fixed

Prefix = '!'

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(msg)
		local args = string.split(msg,' ')
		local commanda = args[1]:sub(#Prefix + 1,#args[1])
		local command = string.lower(commanda)
		if command == "yes" then
			print('yes')
		end
	end)
end)

More of a subpar answer but here I go:

Yikes that is some pretty messy code. Honestly you might want to just rewrite your code instead of trying to fix it and then maintain the mess you currently have.

Try checking out the post @ElliottLMz linked. It’s a great resource and shows a modular and clean workflow you can use.

1 Like

I’ve got the problem of all the code linking together though, so I’m stuck on how I can link all the code together. E.g: The Rank script & commands.

I’m not sure how to effectively answer this question, sorry! I myself use ModuleScripts to link my code together.

You should only do Player.Chatted:Connect(function(message) end) once. In your code you are using it for every command, and every time the player’s character spawns.

Instead, you should connect it once (when the player joins) and then use an if statement for each command (I simplified the command checks for the example):

game.Players.PlayerAdded:Connect(function(player)
   player.Chatted:Connect(function(message)
      if message == "!afk" then
         -- code
      elseif message == "!unafk" then
         -- code
      elseif message == "!host" then
         -- code
      elseif message == "!unhost" then
         -- code
      end
   end)
end)

You do not need to reconnect it every time the player’s character spawns, as player.Chatted has nothing to do with the character.

In addition, the reason only one function works (which wouldn’t actually be a problem with multiple different connections) is because you are not ending the function blocks properly. Note how in the above example, end is used after the if statements finish, and end) is used after the functions inside the :Connect() are finished. Make sure you have those ends put in properly.

3 Likes