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

Can you show a brick's size on a TextLabel?

I'm making this silly game where you press X to increase the size of a brick. I've got that down, but I wanted to make a TextLabel on your screen that shows you the size of the brick. Is it possible to do this? If so, can someone tell me how to do it?

2 answers

This pretty much does everything except grow the brick and save player data to DataStore...

Script in ServerScriptService:

local getPartForPlayer = Instance.new("RemoteFunction")
getPartForPlayer.Name = "GetPartForPlayer"
getPartForPlayer.Parent = game.ReplicatedStorage

local playerParts = {}

getPartForPlayer.OnServerInvoke = function(player)
	local t = tick()
	repeat
		wait()
	until (playerParts[player.Name] or tick() - t > 5)
	return playerParts[player.Name]
end

local function createPartForPlayer(player)
	local playerPart = Instance.new("Part")
	playerPart.Name = player.Name.."'s Part"
	playerPart.Parent = game.Workspace
	playerParts[player.Name] = playerPart
end

game.Players.PlayerAdded:Connect(createPartForPlayer)

game.Players.PlayerRemoving:Connect(function(player)
	-- Bonus: save their part's size in a datastore
	local playerPart = playerParts[player.Name]
	if playerPart then
		playerPart:Destroy()
		playerParts[player.Name] = nil
	end
end)

for _, player in pairs(game.Players:GetPlayers()) do
	createPartForPlayer(player)
end

LocalScript in StarterCharacterScripts:

local playerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local myPart = game.ReplicatedStorage:WaitForChild("GetPartForPlayer"):InvokeServer()

local billboardGui = Instance.new("BillboardGui")
billboardGui.Parent = playerGui
billboardGui.Adornee = myPart
billboardGui.Size = UDim2.new(0, 200, 0, 50)
billboardGui.StudsOffsetWorldSpace = Vector3.new(0, 2, 0)
billboardGui.ExtentsOffsetWorldSpace = Vector3.new(0, 1, 0)

local textLabel = Instance.new("TextLabel")
textLabel.Parent = billboardGui
textLabel.Text = myPart:GetMass()
textLabel.Size = UDim2.new(1, 0, 1, 0)

myPart:GetPropertyChangedSignal("Size"):Connect(function()
	textLabel.Text = tostring(myPart:GetMass())
end)

I'll assume you have the TextLabel on the screen already. Now, you have a couple options:

  1. In the script that increases the brick's size, also update the TextLabel
  2. Use an event to monitor the brick's size.

Both options use a LocalScript.

--Option 1
local brickSizeLabel = game.Players.LocalPlayer.ScreenGui.BrickSizeLabel -- obviously change to fit your situation
brickSizeLabel.Text = "starting size here"
--when you increase the size, say to "size":
brickSizeLabel.Text = size

--Option 2
local brickSizeLabel = ____ -- same as Option 1
local brick = _______ -- You have to figure out how the script knows about this brick. Does it have the same name as the LocalPlayer (or maybe is named "LocalPlayer's Brick"?)
brick.Changed:Connect(function()
	brickSizeLabel.Text = brick.Size.X
end)

3 comments

0

Thanks, Option 2 worked! :D

0

You're welcome! Don't forget to accept an answer to mark the question as solved.

0

Do note that WillieTehWierdo200's answer appears to work for FilteringEnabled places (though I haven't tested it)

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