Help with making Text Fade away after a small delay

I’m making a funtion which fade away the text after a set amount of time passed this “amount of time” reset everytime you type again which is working fine except

if you not noticing already. the text will continue to fade away if you start typing again after the funtion called which is not what I want
I want to make it so everytime you type it reset the debounce and also stop the fading progress

the code:

--Service
local TweenService = game:GetService("TweenService")
local ss = game:GetService("SoundService")

--Empty Variable to use later
local Msg = " "
local Text = script.Parent.Frame.TextLabel
local TextColor = nil

--Variables
local StopSound = false
local debounce = 10

local function FadeText() -- fade away effect
	while Text.TextTransparency < 1 do
		Text.TextTransparency += 1/100
		wait()
		if Text.TextTransparency >= 1 then
			Text.TextTransparency = 1
			Text.Parent.Visible = false
		end
	end
end

local function GetTotalLetterInMessage(message : string) -- get total letter count
	return #message
end

local function CreateGiberish() -- create random letter
	local Giberish = ""
	for i = 1, GetTotalLetterInMessage(Msg) do
		local RandomNumber = math.random(1, 2)
		if RandomNumber == 1 then
			Giberish = Giberish .. string.upper(string.char(math.random(97, 122)))
		else
			Giberish = Giberish .. string.char(math.random(97, 122))
		end
	end
	return Giberish
end

local function Replacing() -- replacing random letter with an action text
	local TextToReplace = Text.Text
	if string.len(TextToReplace) <= 0 then
		warn("You can't replace empty text!")
		return
	end
	Text.Parent.Visible = true
	Text.TextTransparency = 0
	local chars = string.split(TextToReplace, "")
	local num = 0

	while task.wait(0.03) do
		num += 1

		local newText = string.sub(Msg, 0, num)
		for i = num+1, #Msg do
			newText ..= chars[math.random(1,#chars)]
		end
		Text.Font = Enum.Font.SciFi
		Text.TextColor3 = TextColor
		Text.Text = newText

		if num >= GetTotalLetterInMessage(Msg) then
			break
		end
	end
end

game:GetService("ReplicatedStorage").CatchPlrMessage.OnClientEvent:Connect(function(msg, Color) -- RemotEevent
	TextColor = Color
	StopSound = false
	Msg = Text.Text.upper(msg)
	Text.Parent.Visible = true
	Text.Text =  CreateGiberish()
	Text.MaxVisibleGraphemes = 0
	local tween = TweenService:Create(Text, TweenInfo.new(string.len(msg)/50), {MaxVisibleGraphemes = utf8.len(Text.ContentText)})
	tween:Play()
	tween.Completed:Wait()
	Replacing()
	StopSound = true
	Text.MaxVisibleGraphemes = -1
end)

Text:GetPropertyChangedSignal("MaxVisibleGraphemes"):Connect(function() -- check everytime Text got edit
	if StopSound == true then return end
	ss:PlayLocalSound(ss.ScifiClick)
	Text.Font = Enum.Font.Code
	Text.TextColor3 = Color3.new(math.random(), math.random(), math.random())
	debounce = 10
end)

while task.wait(1) do -- timer to start fading
	debounce -= 2
	if debounce <= 0 then
		debounce = 0
		FadeText()
	end
end
1 Like

nvm I just need to make a variable to act as toggle
here’s code if you interested in making something similiar

--Service
local TweenService = game:GetService("TweenService")
local ss = game:GetService("SoundService")

--Empty Variable to use later
local Msg = " "
local Text = script.Parent.Frame.TextLabel
local TextColor = nil
local Cancel = false

--Variables
local StopSound = false
local debounce = 10

local function FadeText() -- fade away effect
	while Text.TextTransparency < 1 do
		if Cancel == true then break end
		Text.TextTransparency += 1/100
		wait()
		if Text.TextTransparency >= 1 then
			Text.TextTransparency = 1
			Text.Parent.Visible = false
		end
	end
end


local function GetTotalLetterInMessage(message : string) -- get total letter count
	return #message
end

local function CreateGiberish() -- create random letter
	local Giberish = ""
	for i = 1, GetTotalLetterInMessage(Msg) do
		local RandomNumber = math.random(1, 2)
		if RandomNumber == 1 then
			Giberish = Giberish .. string.upper(string.char(math.random(97, 122)))
		else
			Giberish = Giberish .. string.char(math.random(97, 122))
		end
	end
	return Giberish
end

local function Replacing() -- replacing random letter with an action text
	local TextToReplace = Text.Text
	if string.len(TextToReplace) <= 0 then
		warn("You can't replace empty text!")
		return
	end
	Text.Parent.Visible = true
	Text.TextTransparency = 0
	local chars = string.split(TextToReplace, "")
	local num = 0

	while task.wait(0.03) do
		num += 1

		local newText = string.sub(Msg, 0, num)
		for i = num+1, #Msg do
			newText ..= chars[math.random(1,#chars)]
		end
		Text.Font = Enum.Font.SciFi
		Text.TextColor3 = TextColor
		Text.Text = newText

		if num >= GetTotalLetterInMessage(Msg) then
			break
		end
	end
end

game:GetService("ReplicatedStorage").CatchPlrMessage.OnClientEvent:Connect(function(msg, Color) -- RemotEevent
	TextColor = Color
	StopSound = false
	Msg = Text.Text.upper(msg)
	Text.Parent.Visible = true
	Text.Text =  CreateGiberish()
	Text.MaxVisibleGraphemes = 0
	local tween = TweenService:Create(Text, TweenInfo.new(string.len(msg)/50), {MaxVisibleGraphemes = utf8.len(Text.ContentText)})
	tween:Play()
	tween.Completed:Wait()
	Replacing()
	StopSound = true
	Text.MaxVisibleGraphemes = -1
end)

Text:GetPropertyChangedSignal("MaxVisibleGraphemes"):Connect(function() -- check everytime Text got edit
	Cancel = true
	if StopSound == true then return end
	ss:PlayLocalSound(ss.ScifiClick)
	Text.Font = Enum.Font.Code
	Text.TextColor3 = Color3.new(math.random(), math.random(), math.random())
	debounce = 10
end)

while task.wait(1) do -- timer to start fading
	debounce -= 2
	if debounce <= 0 then
		debounce = 0
		Cancel = false
		FadeText()
	end
end

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