How do I simplify my dialogue script

I’m making a dialogue script but the actual script looks messy, does anyone know how to simplify it?

local proximityprompt = game.Workspace.Dialog.Chillems.ProximityPrompt
local sfx = script.Parent.clicksound
local text = script.Parent.Frame.Text1
local debounce = false
local waitTime = 20
proximityprompt.Triggered:Connect(function()
	if debounce == false then
		debounce = true
		script.Parent.Visible = true
		text.Visible = true
		text.Text = "G"
		sfx:Play()
		wait()
		text.Text = "Ge"
		sfx:Play()
		wait()
		text.Text = "Get"
		sfx:Play()
		wait(0.1)
		text.Text = "Get o"
		sfx:Play()
		wait()
		text.Text = "Get ou"
		sfx:Play()
		wait()
		text.Text = "Get out"
		sfx:Play()
		wait(0.1)
		text.Text = "Get out o"
		sfx:Play()
		wait()
		text.Text = "Get out of"
		sfx:Play()
		wait()
		text.Text = "Get out of"
		sfx:Play()
		wait(0.1)
		text.Text = "Get out of m"
		sfx:Play()
		wait()
		text.Text = "Get out of my"
		sfx:Play()
		wait(0.1)
		text.Text = "Get out of my r"
		sfx:Play()
		wait()
		text.Text = "Get out of my ro"
		sfx:Play()
		wait()
		text.Text = "Get out of my roo"
		sfx:Play()
		wait()
		text.Text = "Get out of my room"
		sfx:Play()
		wait(2)
		text.Visible = false
		script.Parent.Visible = false
		sfx:Play()
		sfx.Ended:Wait()
		debounce = false
	end
end)

Check this out

https://developer.roblox.com/en-us/articles/animating-text

When repeating code is an issue, most of the time functions and loops are able to reduce the final script size, if used correctly:

local Prompt = workspace.Dialog.Chillems.ProximityPrompt
local SFX = script.Parent.clicksound
local Label = script.Parent.Frame.Text1

local waitTime = 0.2
local text = "Get out of my room"

local Debounce = false
Prompt.Triggered:Connect(function()
	if not Debounce then 
		Debounce = true 
		Label.Visible = true 
		--we use a loop that runs for each character of the dialogue message
		for i = 1, text:len() do 
			local letter = text:sub(i, i)
			Label.Text ..= letter 
			SFX:Play()
			task.wait(waitTime)
		end
		task.wait(2)
		Label.Visible = false 
		script.Parent.Visible = false 
		Debounce = false 
	end
end)
1 Like

I did but I didn’t know how to add a sound to every word

Thank you so much! I’ll keep this in mind

Holy, What are you doing? Why not just use a for loop?