PlayerChatted

Fires when a player sends a chat message

Example usage:

-- Add "/rain {item}" command that rains a resource on all players
Events.PlayerChatted(function(event)
    -- Split out command from args
    local commandArgs = string.split(event.message, " ")
    
    local command = string.lower(commandArgs[1])
    
    -- Check that command is "/rain"
    if string.lower(command) ~= "/rain" then
        return
    end
    
    -- Verify message at least has 2 arguments (command & item type)
    if #commandArgs < 2 then
        MessageService.sendInfo(event.player, 'Format: /rain <Item Type>')
        return
    end
    local itemType = string.lower(commandArgs[2])
    
    -- Check that item exists
    if not ItemType[itemType] then
        MessageService.sendInfo(event.player, 'No item exists named ' .. itemType)
        return
    end
    
    local numDrops = 25
    task.spawn(function()
        for i=1,numDrops,1 do
            for i,player in ipairs(PlayerService.getPlayers()) do
                local entity = player:getEntity()
                if not entity then
                    continue
                end
                
                local pos = entity:getPosition()
                pos = pos + Vector3.new(math.random(-5, 5), 8, math.random(-5, 5))
                ItemService.dropItem(itemType, pos)
            end
            task.wait(0.2)
        end
    end)
end)

Parameters

player: Player

The player that sent the message

message: string

The message sent by the player

Last updated