Code optimization

Title should explain it, I’m just wondering if its possible to optimize my code further? There will be a lot more dialog in this game, I have to check if each box is pressed or not.

local Player = game.Players.LocalPlayer
local Locked = false

local function typeWrite(object, text)
	for i = 1, #text, 1 do
		object.Text = string.sub(text, 1, i)
		game.SoundService.TypeSound:Play()
		task.wait(0.05)
	end
end


workspace:WaitForChild("PictureWallRequest").Touched:Connect(function(Touched)
	if Touched.Parent == Player.Character then
		if Locked == false then
			Locked = true
			wait(.02)
			Player.PlayerGui.Dialog.Enabled = true
			typeWrite(Player.PlayerGui.Dialog.Frame.TextLabel, "* I want to put pictures on this wall.. But I don't know where to find picture frames.")
			
			task.wait(3)
			Player.PlayerGui.Dialog.Enabled = false
			workspace.PictureWallRequest:Destroy()
		end
	end
end)
1 Like

Someone summoned me?

And honestly? Yes, there are quite a lot of ways you can optimize and simplify your code.

local Players = game:GetService("Players")
local SoundService = game:GetService("SoundService")

local TypeSound = SoundService:WaitForChild("TypeSound")::Sound

local Player = Players.LocalPlayer::Player
local PlayerGui = Player:WaitForChild("PlayerGui")::PlayerGui
local Dialog = PlayerGui:WaitForChild("Dialog")::ScreenGui
local Frame = Dialog.Frame::Frame
local TextLabel = Frame.TextLabel::TextLabel
local Locked:boolean = false

local function typeWrite(object:TextLabel, text:string):()
	for i = 1, #text, 1 do
		object.MaxVisibleGraphemes = i
		TypeSound:Play()
		task.wait(0.05)
	end
end

local PictureWallRequest = workspace:WaitForChild("PictureWallRequest")::BasePart
PictureWallRequest.Touched:Connect(function(Touched):()
	if Touched.Parent == Player.Character then
		if Locked then return end
		Locked = true
		Dialog.Enabled = true
		typeWrite(TextLabel, "* I want to put pictures on this wall.. But I don't know where to find picture frames.")

		task.wait(3)
		Dialog.Enabled = false
		PictureWallRequest:Destroy()
	end
end)
2 Likes

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