I need help finding potential issues for this subtitle system

Hello, I made my first subtitle system, while it works great, I am wondering if there might be any issues for using this method. Not really much to say here I just would like to know if I should fix any issues anybody finds. Thanks!

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local MainModule = require(script:WaitForChild("ModuleScript"))

local plr = game:GetService("Players").LocalPlayer
local PlayerGui = plr:WaitForChild("PlayerGui")

local VoiceActingFolder = ReplicatedStorage:FindFirstChild("VoiceActing")
local VA_Event: BindableEvent = VoiceActingFolder:WaitForChild("Event")
local VA_RE: RemoteEvent = VoiceActingFolder:WaitForChild("Messager")

local Subtitles = PlayerGui:WaitForChild("Subtitles")
local Holder = Subtitles:WaitForChild("Holder")

local Texts = {}
local connections = {}

if game:IsLoaded() then
	for _, v in pairs(VoiceActingFolder:GetDescendants()) do
		if v:IsA("Sound") then
			connections[v] = v.Played:Connect(function()
				task.delay(v:GetAttribute("Delay"), function()
					local TextLabel = MainModule:ReturnClone()
					MainModule.Create(TextLabel, v:GetAttribute("Text"), Holder)
					Texts[v] = TextLabel
				end)
			end)
			
			connections[v] = v.Ended:Connect(function()
				task.delay(v:GetAttribute("Delay"), function()
					MainModule.Remove(Texts[v], .5)
				end)
				connections[v]:Disconnect()
			end)
		end
	end
end
1 Like

i believe “if game:IsLoaded() then” may not always be loaded fast enough, so for slower devices you can add this to the script, to replace the if statement.

if not game:IsLoaded() then
	game.Loaded:Wait()
end

so replacing it would make the code look like this:

if not game:IsLoaded() then
	game.Loaded:Wait()
end

for _, v in pairs(VoiceActingFolder:GetDescendants()) do
	if v:IsA("Sound") then
		connections[v] = v.Played:Connect(function()
			task.delay(v:GetAttribute("Delay"), function()
				local TextLabel = MainModule:ReturnClone()
				MainModule.Create(TextLabel, v:GetAttribute("Text"), Holder)
				Texts[v] = TextLabel
			end)
		end)
		
		connections[v] = v.Ended:Connect(function()
			task.delay(v:GetAttribute("Delay"), function()
				MainModule.Remove(Texts[v], .5)
			end)
			connections[v]:Disconnect()
		end)
	end
1 Like

I’m mainly concerned about the connections, will those cause any issues?

i think they are fine but if you would like to optimize code you can just create subtitles within the same code that plays the audios

correction:
i dont think you have to disconnect the functions because im pretty sure the connections disconnect automatically in your case. im not 100% sure though

1 Like

Well I’d like to have some freedom where the audio gets played, so the for loop is there to detect any va audios being played

1 Like

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