For loop stopping after reading 1st value

I am writing a for loop for a quest system, but for some reason it stops after reading only one value. Here is the code bit:

for i, v in pairs(_G.PlayerData[player.Name].Quests) do
			local success, Error = pcall(function()
				print(v.QuestName)
				for j, k in pairs(v.Required) do
					if k.TaskType == "PuntPuntie" then
						if k.ReqType == projectile.Name then
							if k.Finished >= k.Times then
								print("yeah you're done kid")
							else
								k.Finished += 1
								wait(0.01)
								punt3:FireClient(player, v.Required, _G.PlayerData, i)
							end
						end
					elseif k.TaskType == "PuntStuds" then
						if k.ReqType == projectile or k.ReqType == "Any" then
							if k.Finished >= k.Times then
								print("holy mackerel")
							else
								if distanceTraveled >= k.Amount then
									k.Finished += 1
									wait(0.05)
									punt3:FireClient(player, v.Required, _G.PlayerData, i)
								else
									print("not enough sorry kid...")
								end
							end
						end
					else
						warn("hmmm seems im kinda silly")
						print(k.TaskType)
					end
				end
			end)
			
			if Error then
				print("seems you don't have a quest there bud")
			end
		end

I have no idea why this is stopping so soon

1 Like

Try printing _G.PlayerData[player.Name].Quests to see how many values there really are.

I tried this, and there were 3 values. But it still stopped after reading the first value.

Try deleting the pcall and just printing v in the loop just to test.

still didn’t work -_- thx for responding though

Wdym it didn’t work? does it only print one quest? Also replace the loop with:

for i, v in _G.PlayerData[player.Name].Quests do

the j, k loop is the one that is stopping suddenly, and it prints that there are 3 values. However, it still only stops after running through once

should’ve specified which loop was stopping sorry

So when you print v.Required it shows that there are 3 objects? Try deleting sections of the if statement and testing if they obstruct the execution until you narrow it down.

From what I can tell, it’s probably because the code hits an error inside the pcall, and since the loop is inside the pcall, the loop ends without any error message (the pcall is catching it).

if the code above is running, I would replace the print with print(Error) and see what the error message is.

Edit: I would remove the pcall. The code shown doesn’t seem to use anything that would require the use of a pcall. If your code is erroring, analyze and think of a way to write it without pcalls.

And it has been a while since I’ve seen usage of _G. Maybe consider using a module script?

At this point, use a breakpoint and enabled the Watch window. You will see how the script is being executed line-by-line, and what the states of your variables are

1 Like

ok, i am going to test this now I figured how to enable breakpoints turns out the debugger wasn’t enabled

when I tested with the breakpoint, it said that the value of “j” was equal to 3. However, it did not print anything else regarding the first 2 values, which is strange. Any idea on how to fix this?

Either 3 was the first key pairs() found in the provided table, or you put the breakpoint inside an if statement, and the if statement was only true on the third iteration.

I put it in one of the “if” statements, silly me

Once I put the breakpoint outside the “if” statement, it said that j = 1 and k.TaskType = “PuntStuds”. For some odd reason, it did not run the function that it should have when k.TaskType was equal to this, which confuses me quite a bit. Any reason that this may be happening?

I even had it print the value of k.TaskType, and it did but still didn’t run the proper function except for the last value. My head is starting to hurt :skull:

Uhhh… turns out I’m kinda silly and made a mistake in one of my other scripts. But hey, at least I know how to use breakpoints now!

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