How to write this function?

Hello there, thank you for going through my post!

I know to use functions, but I don’t know to use the chat function.

I have been searching tutorials, videos, post, images and more information about it. I want to know how to execute code if someone says something in the chat, a function. How I may write the function for my commands?

Script that of course don’t work:

game.Chat.PlayerChatted:connect(function(message)
	if Player:GetRankInGroup(AllowedOnesGroupId) >= AllowedToHost then
		if Player:Chat ("!Host") then
			game.StarterGui.HostGUI.label.text = ???
		end
	end
end)

I’m guessing that the event that you’re trying to connect to is chatted?

In that case, there is a message parameter (you already have that in your function too) that you can check using something like

if (message == "Command") then
end

or something along those lines. Hope I helped!

2 Likes

Their is multiple issues I found in your script that will lead to issues. I’m just gonna go through all the issues and then give you the edited code.

Issue One: The Event Connection

For any connections for the player chat you need to connect to each player’s .Chatted event. To do this you need to make the connection when they join the game. An example is below.

game.Players.PlayerAdded:Connect(function(player)
   player.Chatted:Connect(function(message)
      --Interact with the message
   end)
end)

Issue 2: :connect instead of :Connect

:connect is depercated and you should be using :Connect instead.

Issue 3: Modifying StarterGui

The GUI’s player’s see are inside of a folder inside of their player. Modifying StarterGui only modifies the template GUI that will be replicated to clients that join the game.

Issue 4: text isn’t a valid property of a TextLabel

When it comes to identifying properties capitalization is very important. You need to use .Text instead.

game.Players.PlayerAdded:Connect(function(player)
   player.Chatted:Connect(function(message)
      if message == "!Host" then
          for _,player in pairs(game.Players:GetChildren()) do
             player.PlayerGui.HostGUI.label.Text = --Text Here
          end
      end
   end)
end)
1 Like

You seem to be a little misled by some of Roblox’s API. You need to use the Player.Chatted event.

For a LocalScript, you can use Players.LocalPlayer to get a reference to player whose client the LocalScript is running on:

local function onLocalPlayerChatted(message)
    -- process the message here
end
game:GetService("Players").LocalPlayer.Chatted:Connect(onLocalPlayerChatted)

However, for a server-side script, you’ll have to detect players entering the game using Players.PlayerAdded, then connect to the Chatted event on those players:

local function onPlayerChatted(player, message)
    -- process the message here
end

local function onPlayerAdded(player)
    player.Chatted:Connect(function (message)
        onPlayerChated(player, message)
    end)
end
game:GetService("Players").PlayerAdded:Connect(onPlayerAdded)
2 Likes