Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to detect when a TextLabel "exits" the frame?

In my custom chat UI, I have a frame for the area of the chat, and my system works in that it adds a text label featuring the player who typed it and the text (or if you want a better visualization, I'm working on a old Roblox recreation, and I'm using the 2006-2009 ish chat system).

What I want to do is detect if a text label has exited the bounds of the frame by any bit, and if so, destroy it. I'm doing this method because of, well, I'm not going to go into detail here, but basically screen sizes and stuff.

Here is my code if that helps.

script.Parent.FocusLost:Connect(function(enter)
	local enteredText = script.Parent.Text
	if enter then
		
		script.Parent.Text = ""
		
		game.ReplicatedStorage.Events.FilterChatMessages:FireServer(enteredText)
	end
end)

game.ReplicatedStorage.Events.SendFilteredMessage.OnClientEvent:Connect(function(username, filteredText)

	local text = Instance.new("TextLabel")
	text.RichText = true
	text.Font = Enum.Font.SourceSans
	text.TextSize = 14
	text.BorderSizePixel = 0
	text.BackgroundColor3 = Color3.fromRGB(180, 180, 180)
	text.TextColor3 = Color3.fromRGB(255, 255, 255)
	text.BackgroundTransparency = 0.5
	text.Text = '<b><i><font color="rgb(255,165,0)">'..username..'; </font> '..filteredText..'</i></b>'
	text.Parent = script.Parent.Parent.ChatArea
	wait()
	text.Size = UDim2.new(0, text.TextBounds.X, 0, text.TextBounds.Y)
end)

Let me know if you need any more information.

3 comments

0

Why not set the frame's ClipDescendants property to true? It will hide any parts of the text label which are out of bounds.

0

ok, but how do i detect it when it does that so i can destroy it?

0

because i think it would lag if there are 3000 chat messages

1 answer

You could do this:

-- stuff

    local text = Instance.new("TextLabel")
    text.Name = "PlayerNameLabel"
    text.RichText = true
    text.Font = Enum.Font.SourceSans
    text.TextSize = 14
    text.BorderSizePixel = 0
    text.BackgroundColor3 = Color3.fromRGB(180, 180, 180)
    text.TextColor3 = Color3.fromRGB(255, 255, 255)
    text.BackgroundTransparency = 0.5
    text.Text = '<b><i><font color="rgb(255,165,0)">'..username..'; </font> '..filteredText..'</i></b>'
    text.Parent = script.Parent.Parent.ChatArea
    wait()
    text.Size = UDim2.new(0, text.TextBounds.X, 0, text.TextBounds.Y)
    script.Parent.Parent.ChatArea.ChildRemoved:Connect(function(child)
        if child:IsA("TextLabel") then
            if child.Name == "PlayerNameLabel" then
                child:Destroy()
            end
        end
    end)
end)


1 comment

0

no, because ChildRemoved is when something has been deleted, ClipDescendants only gets rid of the visibility of the object

This discussion is preserved as it was. New answers, comments, and votes are closed.