While loop not breaking

I want to break this loop, and continue on to the next function, which also has a while loop.

The loop doesn’t seem to be breaking, I checked it a bunch and set up a lot of print statements to check, and the loop does seem to be the problem.

I searched up cases of while loops not breaking, nothing worked and more.

Here is the code

local function EnterArea(AreaObjectValue,customString)
	if db == false then
		db = true
		ReplicatedStorage.Remotes.QuestEvent:FireAllClients('',customString,'')
		AreaObjectValue.Touched:Connect(function() end)
		while wait(0.5) do
		for i,v in pairs(AreaObjectValue:GetTouchingParts()) do
			if v.Parent:FindFirstChildOfClass('Humanoid') and game:GetService('Players'):GetPlayerFromCharacter(v.Parent) and v ~= nil then
					ReplicatedStorage.Remotes.QuestEndEvent:FireAllClients()
					task.wait(1)
					break
			end
			end
		end
	end
end

local function GetitemsToldbyNPC(QuestItemName,customString,totalCollected,endMessage,amountCollected,amountToCollect)
	if db == false then
		db = true
		ReplicatedStorage.Remotes.QuestEvent:FireAllClients(amountCollected,customString,totalCollected)
		for i,v in pairs(children) do
			if v.Name == QuestItemName then
				v.Equipped:Connect(function()
					if v.Parent:FindFirstChildOfClass('Humanoid') then
						if not v:FindFirstChild('collectedtag') then
						local collectedtag = Instance.new('BoolValue')
							collectedtag.Parent = v
							collectedtag.Name = 'collectedtag'
						amountCollected = amountCollected + 1
						ReplicatedStorage.Remotes.QuestEvent:FireAllClients(amountCollected,customString,totalCollected)
						if amountCollected == amountToCollect then
							ReplicatedStorage.Remotes.QuestEndEvent:FireAllClients()
							createDialogueEvent:FireAllClients(endMessage)
							wait(3)
							hideDialogueEvent:FireAllClients()
							questCompleted = true
							end
						end
					end
				end)
			end
		end
	end
end

EnterArea(workspace.QuestBlockFollowRocks,'Follow the rocks and find water')
-- 'hi' is not printed here, the script just breaks at this point.
db = false
print('hi')
GetitemsToldbyNPC('Bucket','Collect 1 bucket ',' /1 Collected','Nice, now we can obtain water, though it might damage us as its unfiltered..',0,1)

db = false

Note: Please ignore the parts where it says stuff about dialogue, that is an irrelevant part and a modified free script I used, I know for a fact that it is not the issue.

You are break for i loop, so while not breaking.

2 Likes