How do I optimize my quest gui script to be better?

This is my first time making a quest gui script and I wanted to know if there are ways to make the code cleaner, fluid, and overall optimized. Heres my script:

local stg = game:GetService("StarterGui")
local ts = game:GetService("TweenService")

script.Parent.ProximityPrompt.Triggered:Connect(function(plr)
	print('triggered')
	local frame = plr.PlayerGui.QuestGui["Quest#1"]:FindFirstChild("DialogFrame")
	local questframe = plr.PlayerGui.QuestGui["Quest#1"]:FindFirstChild("QuestFrame")
	local sounds = game:GetService("ReplicatedStorage").ClonedSounds["Typewriter 2 (SFX)"]
	local label = frame.Dialog
	local no = frame.Nah
	local yes = frame.Yes
	plr.PlayerGui.QuestGui.Enabled = true
	local slow = 0.1
	
	label.MouseButton1Click:Once(function(input)
		print('touching')
		slow = 0
		
	end)
	if plr.PlayerGui.QuestGui.Enabled then
		print('true')
		frame.Visible = true
		for i, v in  pairs(frame:GetChildren()) do
			if v:IsA("TextButton") then
				v.Text = ""
			end
		end
		local function typewrite(obj, text)
			
			for i = 1, #text do
				task.wait(slow)
				obj.Text = string.sub(text, 1, i)
				sounds:Play()
			end
			
		end
		
		local function confirmation(obj)
			if obj == no then
				print('no')
				plr.PlayerGui.QuestGui.Enabled = false
			elseif obj == yes then
				print("Yeah")
				questframe.Visible = true
				frame.Visible = false
			end
		end
		
		typewrite(label, "Can you kill 5 rigs for me?")
		
		if label.Text == "Can you kill 5 rigs for me?" then
			typewrite(no, "Nah I dont wanna")
			typewrite(yes, "HECK YEAH!")
		end
		
		no.MouseButton1Click:Once(function()
			confirmation(no)
		end)
		yes.MouseButton1Click:Once(function()
			confirmation(yes)
		end)
	end
end)
4 Likes
  1. use module scripts for type write effects Functions
    2.Use Remote Events for a communication between server to client
    3.use a Dictonary to store different texts as a key and the value will be a function depending on the outcome yoy want
1 Like

Why use a remote events for communication between server to client when its only client?

its better off to use a script for the proximity prompt then use a remote event to :FireClient()
for the triggered player a quest

Also say you had a huge amount of proximity prompts or an object that will give you quest it would be better to connect those to a remote event so the server requests something to happen on the client side.In Addition,You could use CollectionService to tag proximity prompts or objects related so its more efficient using only one script to handle tagging objects.

it’s pretty much increasing network load, remote event is useless if OP don’t want to perform server logic, proximity prompts can work on server and they are fine

1 Like

Your code is unredable, you don’t use cases for variable names, also some are unclear, like obj or label

My tips:

  • Don’t localize player in proximity prompt, you can do it on top of the script if it’s all on client
  • Use full names TweenService, not ts or DialogFrame, not label
  • Try to not use annonymous functions as they are unclear what they do
  • Don’t hard-code your quests, i see your script only can work with one quest
  • Try to separate functions into modules, if you need to pass objects via connection, you can do something like at the bottom of current script, where you use annonymous function that calls other function

I’ve made scripting tutorial for advanced topics, and some of them might help you create quest system, here is link if you want: Advanced Scripting Tutorial

Remember that refactoring is common practice, and rewriting code to be cleaner is good

True! You are going to be increasing the networkload so it would be better to let the client side handle the quest but wouldnt you want to at least verify on the server that the client completed a quest on the server using a RemoterFunction.

Yeah that basically sums up his problem to write the code more efficient and clean.

Verify is good if there is something to verify, sending remote to a server for the sole purpose of detecting something on a server is useless

Don’t trust the client is over-used, and writing safe code is enough to prevent cheating in game, even if parts of it are on client side

I agree but im saying if the quest involves gameplay from player to achieve the quest and get some type of reward.

Rewards can be tested via stats, and quest can be visual indicator for player

1 Like

Ahhh ok that makes sense thank you

I have actually made a part 2 to this post since I took your suggestions and implemented them into my quest system also I want to know what I can do to make those modules have effecient code heres the link: