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
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
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