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

Why is TextBounds printing 0 when there is a Text inside the Label ?

At Line 63 I print the TextLabel's TextBounds which prints me 0, but in play mode when I manually type it in the command bar it gives me 46, Can someone tell me why it prints 0 when it's done via the script??


-- \\ Services

local rep = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()



-- \\ ColorValue

local ColorValue = char:WaitForChild("ChatColor")


-- \\ Remote Event


local ChatEvent = rep:WaitForChild("ChatEvent")

-- \\ Client

local Client = Players.LocalPlayer
local Mouse = Client:GetMouse()

-- \\ GUIs

local chtbar = script.Parent.Parent:WaitForChild("Chatbar")
local Chatbar = chtbar:WaitForChild("Chatting")
local Chatframe = script.Parent


-- \\ Chat messages

local Chats = {}

-- \\ Push Messages

local function PushMessages(Size)
	for i,v in pairs(Chats) do
		v.Position = v.Position - UDim2.new(0,0,0,Size)
		
		if v.Position.Y.Offset < -Chatframe.AbsoluteSize.Y then
			Chats[i] = nil
			v:Destroy()
		end
	end
end



-- \\ Create Message

local function CreateMessage(Message, Speaker)
	
	local Label2 = Instance.new("TextLabel")
	Label2.Name = "Name"
	Label2.BackgroundTransparency = 1
	Label2.TextXAlignment = "Left"
	Label2.TextColor3 = Color3.new(1,1,1)
	Label2.Font = "ArialBold"
	Label2.FontSize = "Size14"
	Label2.TextColor3 = ColorValue.Value
	Label2.Position = UDim2.new(0,0,1,-25)
	Label2.Text = tostring(Speaker)
	print(Label2.TextBounds)
	Label2.Size = UDim2.new(0, Label2.TextBounds.X + 0.005, 0, 25)
	PushMessages(Label2.Size.Y.Offset)
	Chats[#Chats+1] = Label2
	Label2.Parent = Chatframe
	
	
	local Label = Instance.new("TextLabel")
	Label.Name = "Message"
	Label.BackgroundTransparency = 1
	Label.TextXAlignment = "Left"
	Label.TextColor3 = Color3.new(1,1,1)
	Label.Font = "Legacy"
	Label.FontSize = "Size10"
	Label.Size = UDim2.new(1, 0, 0, 25)
	Label.Position = UDim2.new(0,0,1,-25)
	Label.Text = ": "..Message
	Chats[#Chats+1] = Label
	Label.Parent = Chatframe
	
end	

Chatbar.FocusLost:connect(function(Enter)
	if Enter then
		if Chatbar.Text == "" then
			chtbar.Visible = false
			return
		end
		ChatEvent:FireServer("chat", Chatbar.Text)
		Chatbar.Text = ""
		Chatbar.Visible = false
		chtbar.Visible = false
	end
end)

local UIS = game:GetService("UserInputService")

game:GetService("UserInputService").InputBegan:connect(function(h, i)
	if (h.KeyCode == Enum.KeyCode.Slash) or (h.KeyCode == Enum.KeyCode.LeftShift and UIS:IsKeyDown(Enum.KeyCode.Seven)) or (h.KeyCode == Enum.KeyCode.Seven and UIS:IsKeyDown(Enum.KeyCode.LeftShift)) then
		
		Chatbar.Visible = true
		chtbar.Visible = true
		Chatbar:CaptureFocus()
		
	end
end)


ChatEvent.OnClientEvent:connect(function(Request, Message, Speaker)
	Request = Request:lower()
	
	if Request == "chat" then
		CreateMessage(Message, Speaker)
	end
end)



StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat, false)

1 answer

✓ Accepted answer

What bounds?

When you print it, it isn't Parented to anything. To be able to get the text bounds of something, Roblox needs to know the bounds for it to be rendered to. If it's not parented to anything renderable, to give it the bounds for its Size, it'll have no TextBounds size.

Print it after you've Parented everything.

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