Typewrite function very slow

  1. What do you want to achieve? A text about a quest appearing on a gui

  2. What is the issue? The Typewrite function is VERY slow for some reason

The script (local):

local screengui = script.Parent
local dialogGui = screengui.Dialog
local questcenter = game.ReplicatedStorage.QuestCenter
local player = game.Players.LocalPlayer

local writing = false
local nextChild = nil

function typewrite(object, text)
	writing = true
	for i = 1,#text do
		object.Text = string.sub(text,1,i)
		print("Writing")
		wait(0.05)
	end
	writing = false
end

function findQuest(name)
	for i, v in pairs(questcenter.Quests:GetChildren()) do
		if v.Name == name then
			return v
		end
	end
end

function startQuest(quest)
	player.QuestVariables.PlayerBusyWithQuest.Value = true
	local folder = findQuest(quest.Parent.Name)
	local dialog = folder.Dialog

	dialogGui.Visible = true
	
	nextChild = dialog:GetChildren()

	while wait() do
		for i, v in pairs(nextChild) do
			if v.Name == "Text" then
				if not writing then
					nextChild = v:GetChildren()
					typewrite(dialogGui, v.Value)
				end
			elseif v.Name == "Option1" then
				for i, option in pairs(nextChild) do
					local opt = Instance.new("TextButton", dialogGui.Options)
					opt.Name = option.Name
					opt.TextScaled = true
					opt.Text = option.Value
				end
			end
		end
	end
end

while wait() do
	for i, quest in pairs(questcenter:GetDescendants()) do
		if quest:IsA("RemoteEvent") then
			quest.OnClientEvent:Connect(function(questName)
				if quest.Name == "StartQuest" then
					startQuest(quest)
				elseif quest.Name == "DenyQuest" then
					print("Deny")
				
				end
			end)
		end
	end
end

Thanks for reading and hopefully you can help me.

1 Like

Change the wait in the type writer function

1 Like

Thanks for your reply. To what should I change the wait()? Also, if I completely delete the wait() it works but it immediately writes the text.

1 Like

I found the solution, I changed wait() to task.wait() and it worked.

1 Like

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