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

Custom Chat GUI, player's message appears/sends x the amount of players rather than once. help?

Asked by 8 years ago

So, I'm planning on using a custom chat Gui with the coreGui of Chat Disabled, Currently, I modified the Custom Chat Gui of UristMcSparks in the wiki http://wiki.roblox.com/index.php?title=Custom_chat_GUI by replacing the event of player.Chatted to a server event called SendMessage for filtering enabled. So i made a textbox that whenever you press /, it focuses and when you click enter and the focus is lost, it sends the message to the event. The code works, but whenever a player chats, the message appears x the amount of players, help? (NOTE FILTERING ENABLED IS ON) Server Script

local MAX_MESSAGES = 15
-- a dictionary between players and the list of messages they should see
-- note the use of weak tables (http://wiki.roblox.com/index.php?title=Weak_tables)
-- to save memory, so that message lists are kept only while the player is in game
local messageData = {}

-- function to store filtered message for each player and send update
function storeMessage(sender, message)
    -- loop through all players in the game
    for _, player in pairs(game.Players:GetPlayers()) do
        -- filter message
        local filteredMessage = sender .. ": " .. message
        -- get table containing past messages
        local playerMessages = messageData[player] 
        -- add new message to table
        table.insert(playerMessages, filteredMessage)
        -- check if table is full. If so, remove the oldest message
        if #playerMessages > MAX_MESSAGES then
            table.remove(playerMessages, 1)
        end
        -- send update to the player
    game.ReplicatedStorage.SendMessages:FireClient(player, playerMessages) 
    end
end

-- bind function to when player joins the game
game.Players.PlayerAdded:connect(function(player)
    -- setup table to hold messages for the player
    local playerMessages = {}
    messageData[player] = playerMessages
    -- bind function to player chat
local event = game.ReplicatedStorage.SendMessage
event.OnServerEvent:connect(function(player, message)
        storeMessage(player.Name,  message)
    end)
end)

GUI Local Script

local chatFrame = script.Parent.ChatFrame
 game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)
-- Bind function to update from server
game.ReplicatedStorage.SendMessages.OnClientEvent:connect(function(messages)
    -- Clear all of the current messages
    chatFrame:ClearAllChildren()    
    -- Create TextLabel for every message
    for i, message in pairs(messages) do
        local newLine = Instance.new("TextLabel", chatFrame)
        newLine.Text = message  
        newLine.Size = UDim2.new(1,0,0,18)
        newLine.Position = UDim2.new(0,0,0,18*(i-1))
        newLine.TextXAlignment = Enum.TextXAlignment.Left
        newLine.TextYAlignment = Enum.TextYAlignment.Center
        newLine.BackgroundTransparency = 1
        newLine.BorderSizePixel = 0
        newLine.FontSize = Enum.FontSize.Size18
        newLine.Font = Enum.Font.SourceSans
    end
end)
local Mouse = game.Players.LocalPlayer:GetMouse()
 local TextBox = script.Parent.TextBox
Mouse.KeyDown:connect(function(Key)
    if Key:lower() == "/" then
        TextBox:CaptureFocus()
    end
end)
TextBox.FocusLost:connect(function(enterPressed)
    if enterPressed then
        local Message = TextBox.Text
        local Event = game.ReplicatedStorage.SendMessage
        Event:FireServer(Message)
        Message = ""
        print(Message)
    end
end)

Answer this question