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

Why is my custom chat duplicating chats after death?

Asked by
AZDev 590 Moderation Voter
8 years ago
function NewLabel(parent, msg, player)
    newchatline = Instance.new("TextLabel", parent)
    newchatline.Text = player.Name.. ": " ..msg
    newchatline.Size = UDim2.new(1,0,0,15)
    newchatline.Position = UDim2.new(0,0,1,-15)
    newchatline.Font = "SourceSansBold"
    newchatline.TextColor3 = Color3.new(1,1,1)
    newchatline.TextStrokeTransparency = 0
    newchatline.BackgroundTransparency = 1
    newchatline.BorderSizePixel = 0
    newchatline.FontSize = "Size14"
    newchatline.TextXAlignment = "Left"
    newchatline.TextYAlignment = "Top"
    newchatline.ClipsDescendants = true
    newchatline.Name = "line1"
end

function UpdateOldLabels(Parent)
    for i,v in pairs(Parent:GetChildren()) do
        if v.Name:sub(1,4):lower() == "line" then
            local LineNumber = v.Name:sub(5)
            if LineNumber == "12" then
                v:Destroy()
            else
                v.Name = "line"..tostring(tonumber(LineNumber) + 1)
                v.Position = v.Position - UDim2.new(0,0,0,15)
            end
        end
    end
end

game:GetService("Players").PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        for _,v in ipairs(game:GetService("Players"):GetChildren()) do
            UpdateOldLabels(v:WaitForChild("PlayerGui").ScreenGui.Frame)
            UpdateOldLabels(game:GetService("StarterGui").ScreenGui.Frame)
            NewLabel(v:WaitForChild("PlayerGui").ScreenGui.Frame, msg, player)
            NewLabel(game:GetService("StarterGui").ScreenGui.Frame, msg, player)
        end
    end)
end)

I used the wiki tutorial to create this. When the player dies all old chats are duplicated. This is not really a major problem but it is annoying.

Below are my attempts to correct this.

game:GetService("Players").PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        for _,v in ipairs(game:GetService("Players"):GetChildren()) do
            UpdateOldLabels(v:WaitForChild("PlayerGui").ScreenGui.Frame)
        -- UpdateOldLabels(game:GetService("StarterGui").ScreenGui.Frame)
            NewLabel(v:WaitForChild("PlayerGui").ScreenGui.Frame, msg, player)
            NewLabel(game:GetService("StarterGui").ScreenGui.Frame, msg, player)
        end
    end)
end)

Commenting this line stopped the duplication of old chats but moved them all to the same line.


game:GetService("Players").PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
        for _,v in ipairs(game:GetService("Players"):GetChildren()) do
            UpdateOldLabels(v:WaitForChild("PlayerGui").ScreenGui.Frame)
            UpdateOldLabels(game:GetService("StarterGui").ScreenGui.Frame)
            NewLabel(v:WaitForChild("PlayerGui").ScreenGui.Frame, msg, player)
            --NewLabel(game:GetService("StarterGui").ScreenGui.Frame, msg, player)
        end
    end)
end)

Commenting that line caused all old chats to be removed on death.

I haven't done custom chat before so any help will be appreciated. Thanks!

1 answer

Log in to vote
0
Answered by 8 years ago

The reason why the chats are duplicating is because you're updating the old labels and making the new labels inside of your for loop. With this knowledge, you should know that the amount of duplications would increase if there were more players in the game.

To fix this, just move the code to update the old labels and add new labels for the StarterGui outside of the for loop. This will only add a new label and update the old labels once, preventing the chat messages from duplicating.


game:GetService("Players").PlayerAdded:connect(function(player)
    player.Chatted:connect(function(msg)
    UpdateOldLabels(game:GetService("StarterGui").ScreenGui.Frame)
    NewLabel(game:GetService("StarterGui").ScreenGui.Frame, msg, player)
        for _,v in ipairs(game:GetService("Players"):GetChildren()) do
            UpdateOldLabels(v:WaitForChild("PlayerGui").ScreenGui.Frame)
            NewLabel(v:WaitForChild("PlayerGui").ScreenGui.Frame, msg, player)
        end
    end)
end)

I hope my answer helped you. If it did, be sure to accept it.

Ad

Answer this question