How to add Spaces between TextButton Text

i was Thinking About Adding a small feature for My Game’s TextButton
which is an effect To The Button’s Text

i want it To Add spaces Between The Button’s Text When The Mouse Hovers over it
like That one in a Game Called "Those Who Remain

i tried Something Like That

script.Parent.MouseEnter:Connect(function()
	script.Parent.Text = "P l a y"
end)

script.Parent.MouseLeave:Connect(function()
	script.Parent.Text = "Play"
end)

The Only Problem With This Script is That I don’t feel it is The Most Efficient way to do it.
if anybody Could Help
that Would be Appreciated

1 Like

I think you could use multiple TextLabels and tween the padding between them, but I don’t think there is a better way to do it.

1 Like

i was thinking about having an in pairs loop That adds Spaces Between The Button Text Characters

1 Like
local text = "Play"
local spacedText = ""
for i = 1, #text do
    spacedText = spacedText .. text:sub(i, i) .. " "
end
script.Parent.Text = spacedText

Try this, I would recommend putting it inside a function if you want the code to be reusable and more compact. Like this:

function onMouseEvent(eventType)
    local text = "Play"
    if eventType == "MouseEnter" then
        text = "P l a y"
    end
    local spacedText = ""
    for i = 1, #text do
        spacedText = spacedText .. text:sub(i, i) .. " "
    end
    script.Parent.Text = spacedText
end

script.Parent.MouseEnter:Connect(function()
    onMouseEvent("MouseEnter")
end)
script.Parent.MouseLeave:Connect(function()
    onMouseEvent("MouseLeave")
end)

You can also have the "Play" and "P l a y" be a function parameter. Although it’s really optional and up to you.

local str = "Play"
local x = str:gsub("%w", "%1 ")