Typewriter Effect Help

Hey there, I’m trying to make it so if the user presses the space bar or clicks the gui displaying the text it skips the effect.

I wrote out this function but have no idea how to include the keycode/mouse inputs

local function TypeText(content)
	for i = 1, string.len(content) do
		frame:WaitForChild("Text").Text = string.sub(content, 1, i)
		wait(0.05)
	end
end

A possible way to make it, is by a variable wich gets changed by an input. Let’s say I press E to skip, and then I would change the variable which for this as an example is CurrentText, and in the for i loop it will check if it is the same.

local CurrentText = "Hello World!"
local function TypeText(content)
    local x = CurrentText
	for i = 1, string.len(content) do
        if x ~= CurrentText then return end
		frame:WaitForChild("Text").Text = string.sub(content, 1, i)
		wait(0.05)
	end
end

game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key)
if key == "E" then
CurrentText = "Bye World!"
end
end)
1 Like

I was trying to make it so the text is typed out like usual then if the hotkey is pressed then the typewriter effect is skipped and the completed text is displayed.

I think that’s what nayro007 did, he added a hotkey “E” that once it is pressed down, skips by displaying the final text.
What I might add is that add a boolean, so if E is pressed, then the boolean is true, and the function is paused. But, this would not be good if you have huge chunks of text. Something like this.

Credits to nayro007, I just added a few things on top of their code.

local CurrentText = "Hello World!"
local isRunning = true -- this is the boolean 

local function TypeText(content)
    local x = CurrentText
	for i = 1, string.len(content) do
        if x ~= CurrentText or not isRunning then return end
		frame:WaitForChild("Text").Text = string.sub(content, 1, i)
		wait(0.05)
	end
end

game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key)
    if key == "E" then
        CurrentText = "Bye World!"
        isRunning = false
    end
end)

-- this just resets everything after its done
isRunning = true
TypeText(CurrentText)

These are just a few additions, but nayro007 solved this issue.
All I’m getting is that the hotkey skips the text, and that is what nayro007’s code does.

If your vision was not achieved, then you have to be more specific in explaining what exactly you want (expand more on what you want if your problem was not solved)

Thank you and good luck!

2 Likes

the other replies seem to have fixed your issue but i’m going to add my solution anyways!
I’m unsure why you’re not just using the MaxVisibleGraphemes property of your text label it’s perfect for a typewriter and it can be tweened!
anyways here is the code

local TweenService = game:GetService("TweenService")
local UserInputService = game:GetService("UserInputService")

local textLabel = script.Parent.TextLabel

local textToDisplay = "Hello, this is a test string for the typewriter"
textLabel.Text = textToDisplay

local tweenInfo = TweenInfo.new(
	4,
	Enum.EasingStyle.Sine,
	Enum.EasingDirection.Out
)

local tween = TweenService:Create(textLabel, tweenInfo, {
	MaxVisibleGraphemes = utf8.len(textLabel.ContentText),
})

local connection = UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Space or input.UserInputType == Enum.UserInputType.MouseButton1 then
		tween:Cancel()
	end
end)

tween:Play()
tween.Completed:Wait()

connection:Disconnect()
textLabel.MaxVisibleGraphemes = -1
1 Like

I turned it into a function and it works perfectly! Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.