Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What Is Wrong With This Chat Script's Private Message Function??

Asked by 7 years ago
Edited 7 years ago

I Was Making A Private Message Function For My Chat But I Dont Know How To Only Get The Players Name And Then Get The Message After That, I Already Started With The Function So Here Is The Script: (and one error Saying: Line 132 bad argument #1 to "find" (string expected, got Object))

-- ROBLOX Services
local ContextActionService = game:GetService("ContextActionService")

-- Static variables
local MAX_MESSAGES = 5
local MESSAGE_HEIGHT = 25
local SpecialPlayers = {[148430515]='Co-Founder',[34929389]='Founder/Lead Developer'}
local PlayerColors = {[148430515]=Color3.new(255,170,0),[34929389]=Color3.new(255,0,0)}

-- Local variables
local player = game:GetService('Players').LocalPlayer
local messages = {}
local chatMessageEvent = game:GetService('ReplicatedStorage').CustomChat.PublicChat

-- Variables for GUI elements
local chatScreen = script.Parent.MainChat
local chatFrame = chatScreen.ChatFrame
local chatInput = script.Parent.ChatBar
local messageFrame = chatFrame
-- Make a copy of the message that will be used later
local messageTemplate = messageFrame.Message:Clone()
messageFrame.Message:Destroy()


local function addMessage(sender, message)
    -- Check if the number of messages has hit the maximum
    if #messages >= MAX_MESSAGES then
        -- If so add extra to the chatFrame
        chatFrame.CanvasSize = UDim2.new(0,0,chatFrame.CanvasSize.Y.Offset + 25,0)
        table.remove(messages,#messages)
    end

    -- Shift all of the messages up one slot
    for i = 1, #messages do
        local y = (MAX_MESSAGES - i - 1) * MESSAGE_HEIGHT
        messages[i].Position = UDim2.new(0, 0, 0, y)
    end 

    -- Create new message GUI elements and add to the message table
    local newMessage = messageTemplate:Clone()
    if sender.UserId == 34929389 then
    newMessage.Text = "Founder"..sender.Name .. ": "
    newMessage.Msg.Text = message
    newMessage.Parent = messageFrame
    newMessage.TextColor3 = Color3.new(255,0,0)
    newMessage.Position = UDim2.new(0, 0, 0, (MAX_MESSAGES - 1) * MESSAGE_HEIGHT)
    table.insert(messages, 1, newMessage)
elseif sender.UserId == 148430515 then
    newMessage.Text = "Co-Founder"..sender.Name .. ": "
    newMessage.Msg.Text = message
    newMessage.Parent = messageFrame
    newMessage.TextColor3 = Color3.new(255,170,0)
    newMessage.Position = UDim2.new(0, 0, 0, (MAX_MESSAGES - 1) * MESSAGE_HEIGHT)
    table.insert(messages, 1, newMessage)
else
    newMessage.Text = sender.Name .. ": "
    newMessage.Msg.Text = message
    newMessage.Parent = messageFrame
    newMessage.Position = UDim2.new(0, 0, 0, (MAX_MESSAGES - 1) * MESSAGE_HEIGHT)
    table.insert(messages, 1, newMessage)
    end
end

local function serverBroadcast(message)
    -- Check if the number of messages has hit the maximum
    if #messages >= MAX_MESSAGES then
        -- If so add extra to the chatFrame
        chatFrame.CanvasSize = UDim2.new(0,0,chatFrame.CanvasSize.Y.Offset + 25,0)
        table.remove(messages,#messages)
    end

    -- Shift all of the messages up one slot
    for i = 1, #messages do
        local y = (MAX_MESSAGES - i - 1) * MESSAGE_HEIGHT
        messages[i].Position = UDim2.new(0, 0, 0, y)
    end 

    -- Create new message GUI elements and add to the message table
    local newMessage = messageTemplate:Clone()
    newMessage.Text = "[Server]"
    newMessage.Msg.Text = message
    newMessage.Parent = messageFrame
    newMessage.TextColor3 = Color3.new(255,0,0)
    newMessage.Position = UDim2.new(0, 0, 0, (MAX_MESSAGES - 1) * MESSAGE_HEIGHT)
    table.insert(messages, 1, newMessage)
    end

local function addPrivateMessage(message,ToPlayer,FromPlayer) -- this is the function that errors
    if ToPlayer == player.Name then
    -- Check if the number of messages has hit the maximum
    if #messages >= MAX_MESSAGES then
        -- If so add extra to the chatFrame
        chatFrame.CanvasSize = UDim2.new(0,0,chatFrame.CanvasSize.Y.Offset + 25,0)
        table.remove(messages,#messages)
    end

    -- Shift all of the messages up one slot
    for i = 1, #messages do
        local y = (MAX_MESSAGES - i - 1) * MESSAGE_HEIGHT
        messages[i].Position = UDim2.new(0, 0, 0, y)
    end 

    -- Create new message GUI elements and add to the message table
    local newMessage = messageTemplate:Clone()
    newMessage.Text = "[From >"..FromPlayer.."> You]"
    newMessage.Msg.Text = message
    newMessage.Parent = messageFrame
    newMessage.TextColor3 = Color3.new(255,0,0)
    newMessage.Position = UDim2.new(0, 0, 0, (MAX_MESSAGES - 1) * MESSAGE_HEIGHT)
    table.insert(messages, 1, newMessage)
    end
    end

-- Function when the input TextBox looses focus
local function onFocusLost(enterPressed, inputObject)
    -- Check if TextBox lost focus because the user pressed "Enter"
    if enterPressed then
            -- check if the player sent a server broadcast
            if player.UserId == 34929389 or player.UserId == 148430515 then
                local cmd = string.sub(chatInput.Text,1,11)
                local ChatCmd = string.lower(cmd)
                if ChatCmd == "/broadcast " then
                    local msg = string.sub(chatInput.Text,11)
                    serverBroadcast(msg)
                end
            end
            local Msgr = string.sub(chatInput.Text,1,5)
            local MsgCmd = string.lower(Msgr)
            if MsgCmd == "/msg " then
                local players = game:GetService('Players'):GetPlayers()
                for index, value in ipairs(players) do
                local Player = string.sub(chatInput,string.find(chatInput, value))
                local Msg = string.sub(chatInput, Player)
                local From = player.Name
                game:GetService('ReplicatedStorage').CustomChat.PrivateChat:FireClient(Msg, Player, From)
                end
            end
            local cmd = string.sub(chatInput.Text,1,11)
            local ChatCmd = string.lower(cmd)
            if ChatCmd ~= "/broadcast " then
            -- Add the message to the GUI (no need to filter messages from the local player)
            addMessage(player, chatInput.Text)
            -- Send message to the server to get filtered and sent to other players
            chatMessageEvent:FireServer(chatInput.Text)
            -- Reset TextBox text
            chatInput.Text = ""
            chatInput.Visible = false
            chatInput.Text = ""
            chatInput.Visible = false
            chatInput.Text = ""
            chatInput.Visible = false
            chatInput.Text = ""
            chatInput.Visible = false
            chatInput.Text = ""
            chatInput.Visible = false
            end
        end
    end

-- Function when the player presses the slash key
local function onSlashPressed(actionName, inputState, inputObject)
    if inputState == Enum.UserInputState.End then
        -- If key up then capture focus in the TextBox so the user can start typing
        chatInput:CaptureFocus()
        chatInput.Visible = true
    end
end

-- Disable ROBLOX default chat
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)

-- Bind functions
chatMessageEvent.OnClientEvent:connect(addMessage)
game:GetService('ReplicatedStorage').CustomChat.PrivateChat.OnClientEvent:connect(addPrivateMessage)
chatInput.FocusLost:connect(onFocusLost)
ContextActionService:BindAction("Chatting", onSlashPressed, false, Enum.KeyCode.Slash)
0
on line 132, "local Player = string.sub(chatInput,string.find(chatInput, value))". I believe it should be "local Player = string.sub(chatInput,string.find(chatInput.value))" xXxDaBossElmoxXx 22 — 7y
0
no, the value is the player that it gets from the for loop Thegrimdeathzombie 40 — 7y

Answer this question