Detect if a folder in a player exists

Hello, I am making a script where if a player has a folder (in their hierarchy ex Player.Folder) then the script ends. I want to use :WaitForChild() but I don’t want there to be an infinite yield, because if the player doesn’t have the folder then the code continues. How can I achieve this?

It keeps saying QuestProgress does not exist.

task.wait(4)

local Dialogue1 = game.Workspace.TriggerDialogue1.ProximityPrompt
local RS = game:GetService("ReplicatedStorage")
Dialogue1.Triggered:Connect(function(plr)
	
	if not plr.QuestProgress then
		local QuestProgress = Instance.new("Folder")
		QuestProgress.Name = "QuestInProgress"
		QuestProgress.Parent = plr


		local playerData = plr
		plr.PlayerGui.Dialogue.Enabled = true

		local UI = plr.PlayerGui.Dialogue

		local NPCNAME = "Pheonix"
		local Dialogue = "Hey, you. I need a favor."
		local Dialogue2 = "You see those guys there with the (color here) shirt?"
		local Dialogue3 = "I need you to take out 10 of them. I'll give you 50 street credit to start you out."

		UI.NPCname.Text = NPCNAME

		for i = 1, #Dialogue do
			UI.Dialogue.Text  = string.sub(Dialogue, 1, i)
			task.wait(0.05)
		end

		repeat task.wait()
		until UI.Value.Value == true

		for i = 1, #Dialogue2 do
			UI.Dialogue.Text  = string.sub(Dialogue2, 1, i)
			task.wait(0.05)
		end

		repeat task.wait()
		until UI.Value.Value == true

		for i = 1, #Dialogue3 do
			UI.Dialogue.Text  = string.sub(Dialogue3, 1, i)
			task.wait(0.05)
		end

		RS.Quests.Pheonix.GivePheonixQuest:FireServer()
	else 
		local KC = plr.KillCount

		if KC.Value >= 6 then
			RS.Quests.Pheonix.FinishPheonixQuest:FireServer()
			local UI = plr.PlayerGui.Dialogue

			local NPCNAME = "Pheonix"
			local Dialogue = "Thanks for that, 'ppreciate it."
			local Dialogue2 = "Heres your 50 street cred, as I said earlier."
			local Dialogue3 = "I'll let you know if I need anything else."

			UI.NPCname.Text = NPCNAME

			for i = 1, #Dialogue do
				UI.Dialogue.Text  = string.sub(Dialogue, 1, i)
				task.wait(0.05)
			end

			repeat task.wait()
			until UI.Value.Value == true

			for i = 1, #Dialogue2 do
				UI.Dialogue.Text  = string.sub(Dialogue2, 1, i)
				task.wait(0.05)
			end

			repeat task.wait()
			until UI.Value.Value == true

			for i = 1, #Dialogue3 do
				UI.Dialogue.Text  = string.sub(Dialogue3, 1, i)
				task.wait(0.05)
			end
		end
	end
end)


	
1 Like

Use Instance:FindFirstChild(InstanceName).

Now you just have to do:

Player:FindFirstChild("FolderName")--  and you're done.

you can use threads for this.

local thread = task.spawn(function()
– code here
end)

whenever you detect the folder (either wait for child or child added event)
do pcall(task.cancel, thread)

edit: you may also have to do some clean up as this just abruptly stops the code and doesnt let it end

How would I implement this to my script?

I’m trying to use if then else, how would I use FindFirstChild for that?

oh wait, on line 7 you need to do

if not plr:FindFirstChild("QuestProgress") then
1 Like

Seems like Hzodx has already answered your question from me. Also next time, please use google. I am sure if you try different queries then you can get your answer!