• Studio

  • Bots

  • Web API

  • Designer Resources

  • Host Resources

  • API

    Services

    Edit

    Chat

    The Chat service provides functionality for creating and managing chat channels to dictate how players communicate with each other, as well as how receiving messages work in the game.

    Properties

    allChannels

    ChannelInfo[]

    ServerOnly

    All chat channels that exist in the game.

    allJoinedChannels

    ChannelInfo[]

    All chat channels that the player has joined.

    An event that is fired when a client receives a text message. If this event has no connected listeners, the text message will display normally.

    If you connect to this event to do custom messaging logic, use the DisplayTextMessage method to send the final results.

    Class.ChannelInfo, Class.Player (the sender), and string are passed as the parameters.

    Chat.TextMessageReceivedHandler:Connect(function(channel, player, message)
      Chat:DisplayTextMessage(channel, player, message)
    end)
    

    An event that is fired when a client receives a whisper. If this event has no connected listeners, the whisper will display normally.

    If your game does not allow whispering at certain times or only with certain people, this event should be connected to and use the DisplayTextMessage method only when whispering is allowed.

    Class.ChannelInfo, Class.Player (the sender), and string are passed as the parameters.

    local canWhisper = false
    Chat.WhisperReceivedHandler:Connect(function(channel, player, message)
      if canWhisper then
        Chat:DisplayTextMessage(channel, player, message)
      end
    end)
    

    An event that is fired by the server to all clients in the same channel as the player that joined. Class.ChannelInfo and Class.Player are passed as the parameters.

    Chat.PlayerJoinedChannel:Connect(function(channel, player)
      Chat.TextMessageReceivedHandler:Connect(function(channel, player, message)
        if message == "Hello" then
          Chat:DisplayTextMessage(channel, player, "Hello, " .. player.name .. "!")
        end
      end)
    end)
    

    An event that is fired by the server to all clients in the same channel as the player that left. Class.ChannelInfo and Class.Player are passed as the parameters.

    Chat.PlayerLeftChannel:Connect(function(channel, player)
      print(player.name .. " has left the channel.")
    end)
    

    An event that is fired by the server to all clients when a channel is destroyed. Class.ChannelInfo is passed as the parameter.

    Chat.ChannelDestroyed:Connect(function(channel)
      print("Channel " .. channel.name .. " has been destroyed.")
    end)
    

    Methods

    ServerOnly

    Creates a new channel. If the channel already exists, it will return the existing channel.

    local isGameRunning = true
    
    local ghostChannel = Chat:CreateChannel("Ghost", true, false)
    local generalChannel = Chat:CreateChannel("General", true, true)
    
    server.PlayerConnected:Connect(function(player)
      if isGameRunning then
        Chat:AddPlayerToChannel(generalChannel, player) -- General channel is for players who are in the game
      else
        Chat:AddPlayerToChannel(ghostChannel, player) -- Ghost channel is for players who are not in the game
      end
    end)
    

    Parameters

    channelName

    string

    allowText

    boolean

    allowVoice

    boolean

    ServerOnly

    Destroys the given channel, removing all players from it.

    local generalChannel = Chat:CreateChannel("General", true, true)
    
    server.PlayerConnected:Connect(function(player)
      Chat:AddPlayerToChannel(generalChannel, player)
    end)
    
    server.PlayerDisconnected:Connect(function(player)
      Chat:RemovePlayerFromChannel(generalChannel, player)
    end)
    
    server.PlayerLeft:Connect(function(player)
      Chat:DestroyChannel(generalChannel)
    end)
    

    Parameters

    Returns

    void

    ServerOnly

    Adds the given player to the given channel.

    local channel = Chat:CreateChannel("General", true, true)
    
    server.PlayerConnected:Connect(function(player)
      Chat:AddPlayerToChannel(channel, player)
    end)
    

    Parameters

    player
    Player

    Returns

    void

    ServerOnly

    Removes the given player from the given channel.

    local isGameRunning = true
    
    local function ResetGame(players)
      for k, v in pairs(players) do
        Chat:RemovePlayerFromChannel(generalChannel, v)
        Chat:AddPlayerToChannel(ghostChannel, v)
      end
    end
    

    Parameters

    player
    Player

    Returns

    void

    Sets the default channel for the player. This will be the default channel text messages are sent to.

    local generalChannel = Chat:CreateChannel("General", true, true)
    
    server.PlayerConnected:Connect(function(player)
      Chat:SetDefaultChannel(player, generalChannel)
    end)
    

    Parameters

    channelInfo
    ChannelInfo

    Returns

    boolean

    Displays the given text message to the channel's chat log with a speech bubble over the given player's head.

    local generalChannel = Chat:CreateChannel("General", true, true)
    
    server.PlayerConnected:Connect(function(player)
      Chat:AddPlayerToChannel(generalChannel, player)
    end)
    
    Chat.TextMessageReceivedHandler:Connect(function(channel, player, message)
      Chat:DisplayTextMessage(channel, player, message)
    end)
    

    Parameters

    from
    Player
    message

    string

    Returns

    void

    Updated about 1 month ago

    PocketWorlds Icon

    © 2024 Pocket Worlds. All rights reserved.