Quest system lagging because of this loop

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

I want to achieve a working quest system

  1. What is the issue?
    the issue is this is constantly looping which is making my game lag how do i have so it works but its not in a loop idk how to explain

  2. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    Haven’t found a solution yet

while task.wait() do
	local Module = require(game.ReplicatedStorage.Quest)
	local plr = script.Parent.Parent.Parent.Parent.Parent.Parent
	if plr.Quests[script.Parent.Name].Min.Value >=plr.Quests[script.Parent.Name].Max.Value then
		plr.Quests[script.Parent.Name].Min.Value = plr.Quests[script.Parent.Name].Max.Value
		if plr.Quests[script.Parent.Name].Completed.Value == false then
			script.Parent.CompleteButton.Visible = true
			if script.Parent.CompleteButton.Visible == true then
				plr.PlayerGui["Screen side buttons"]:WaitForChild("Frame"):WaitForChild("Quests"):WaitForChild("Info").Visible = true
			end
			script.Parent.Bars.TextLabel.Text = "%100"
		else
			script.Parent.CompleteButton.Visible = false
			script.Parent.Bars.TextLabel.Text = "%100"
			script.Parent.Rarity.Text = "Completed!"
			script.Parent.CColor.BackgroundColor3 = game.ReplicatedStorage.Pets.Rarities2.Completed.Color.Value
			script.Parent.Bars.Visible = false
		end
	else
		script.Parent.CompleteButton.Visible = false
	end
end
2 Likes

Jeez dude
You should be learning basics of Luau and roblox api instead of making a game.
Just… why… what have you done that? :skull:
You understand that you can’t learn language or programming for 1~ month and rush into production already?

I think he is more experienced than you

4 Likes

thank you but this script is bugging me

In general, you’ll want to Connect to Events, instead of running checks in a loop. I’ll provide a slightly cleaner version of your script, but please note I’ve just directly translated your logic, so any bugs in your code will persist. It’s hard to fix the bugs without more context about how your quest system works, like

  • What is min value? Is it the current quest progress? How does it get updated?
  • How does the completed value get updated?
  • Note this code will run twice if the min value goes over max value, because it fires for the initial change and then again when it gets clamped to the max value
  • Nitpick: A little weird to have the % before the number
  • Not all properties get reset, if you ever wanted to go back to the state before a quest was completed
  • Unsure where this code runs so I’m unsure if or where you need :WaitForChild vs assuming the children exist
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- What's this doing the return value isn't used anywhere?
local Quest = require(ReplicatedStorage.Quest) 

-- Where is this script running? It's generally better to access via PlayerService to avoid long .Parent chains like this
local player = script.Parent.Parent.Parent.Parent.Parent.Parent

local minValueObject = player.Quests[script.Parent.Name].Min
local maxValueObject = player.Quests[script.Parent.Name].Max
local completedValueObject = player.Quests[script.Parent.Name].Completed

local completeButton = script.Parent.CompleteButton
local bars = script.Parent.Bars
local rarity = script.Parent.Rarity
local cColor = script.Parent.CColor
local completedColor = ReplicatedStorage.Pets.Rarities2.Completed.Color.Value
local screenSideButtons = player.PlayerGui["Screen side buttons"].Frame.Quests.Info

local function updateQuest()
	if minValueObject.Value >= maxValueObject.Value then
		minValueObject.Value = maxValueObject.Value
		
		local isCompleted = completedValueObject.Value
		completeButton.Visible = not isCompleted
		bars.TextLabel.Text = "%100"

		if isCompleted then
			rarity.Text = "Completed!"
			cColor.BackgroundColor3 = completedColor
			bars.Visible = false
		else
			screenSideButtons.Visible = true
		end
	end
end

-- Call once at the start
updateQuest()

-- Listen for changes in quest progress and call again
minValueObject.Changed:Connect(updateQuest)
maxValueObject.Changed:Connect(updateQuest)

There isnt any bug in my code it works fine its just the while task() isnt effient like itll make my game lag if its contantly looping if you know what i mean

The main takeaway from my refactor is to learn to listen for events and react to them rather than running logic on a loop.

If your code was working for you, then the code I gave you should also work, and it uses events to know when to call it again.

Ill try this when i get on pc could u help me on my other post ive got kind of the same issue please