I have been having this problem for a while now, but i dont know why its happening.
so basically, this is for a gun script, and RayInfo is a table in which it includes all the gun’s pellet data. When i try to loop through the table, the in pairs loop breaks early for some reason.
It seems like the reason this is happening is because either TargetPart, TargetPosition, TargetNormal, or DamageToDeal equals nil, causing it to break.
Is this a roblox glitch?
-- in the local script
for i=1, numberOfPellets do
table.insert(rayInfo, {
['TargetPart'] = targetPart2;
['TargetPosition'] = targetPosition2;
['TargetNormal'] = targetNormal2;
['DamageToDeal'] = damageAmount
})
end
--later, in the server script...
print(#RayInfo) -- Prints 6, for 6 pellets in RayInfo, which is correct.
for _, v in pairs(RayInfo) do
print("LOOPED") -- Only prints this 4 times in the output, not 6
local distanceFromPart = (v['TargetPart'].Position - player.Character.Head.Position).Magnitude
if (distanceFromPart > antiExploitCertainMaxRange * 1.5) or (distanceFromPart > antiExploitCertainMaxRange * 1.5) then
print("RETURNED FIRST CHECK") -- Doesn't print at all
return
end
if v['TargetPart']:IsDescendantOf(workspace.Enemies) then
if (determineIfGotHeadshot(v['TargetPart']) and v['DamageToDeal'] > antiExploitCertainShootDamage * IMPORTANTSERVERDATA.HEADSHOT_MULTIPLIER_GUN) or (not determineIfGotHeadshot(v['TargetPart']) and v['DamageToDeal'] > antiExploitCertainShootDamage) then
print("RETURNED SECOND CHECK") -- Doesn't print at all
return
end
end
end
okay, i just tried it, but the loop still breaks. Did you mean to use continue in place of return?
it always breaks as soon as v[‘TargetPart’] equals nil.
for _, v in pairs(RayInfo) do
print(v['TargetPart']) -- when this prints nil, the loop stops.
print("LOOPED") -- Only prints this 4 times in the output, not 6
local distanceFromPart = (v['TargetPart'].Position - player.Character.Head.Position).Magnitude
if (distanceFromPart > antiExploitCertainMaxRange * 1.5) or (distanceFromPart > antiExploitCertainMaxRange * 1.5) then
print("RETURNED FIRST CHECK") -- Doesn't print at all
return
end
if v['TargetPart']:IsDescendantOf(workspace.Enemies) then
if (determineIfGotHeadshot(v['TargetPart']) and v['DamageToDeal'] > antiExploitCertainShootDamage * IMPORTANTSERVERDATA.HEADSHOT_MULTIPLIER_GUN) or (not determineIfGotHeadshot(v['TargetPart']) and v['DamageToDeal'] > antiExploitCertainShootDamage) then
print("RETURNED SECOND CHECK") -- Doesn't print at all
return
end
end
end
Are you getting any errors though? I missed this the first time I read your question, but if your print statements just before the returns aren’t printing, then the return might not be the issue here. An error will stop your code. If v['TargetPart'] returns nil, then v['TargetPart']:IsDescendantOf(workspace.Enemies) will certainly error.
okay i just found the solution, but am still confused why it works like this.
I did:
for _, v in pairs(RayInfo) do
print("LOOPED") -- Prints all 6 times finally
if v['TargetPart'] then -- solution
local distanceFromPart = (v['TargetPart'].Position - player.Character.Head.Position).Magnitude
if (distanceFromPart > antiExploitCertainMaxRange * 1.5) or (distanceFromPart > antiExploitCertainMaxRange * 1.5) then
print("RETURNED FIRST CHECK") -- Doesn't print at all
return
end
if v['TargetPart']:IsDescendantOf(workspace.Enemies) then
if (determineIfGotHeadshot(v['TargetPart']) and v['DamageToDeal'] > antiExploitCertainShootDamage * IMPORTANTSERVERDATA.HEADSHOT_MULTIPLIER_GUN) or (not determineIfGotHeadshot(v['TargetPart']) and v['DamageToDeal'] > antiExploitCertainShootDamage) then
print("RETURNED SECOND CHECK") -- Doesn't print at all
return
end
end
end
end
the reason im still confused is because when i didnt have “if v[‘TargetPart’] then”, it didn’t error when i did “local distanceFromPart = v[‘TargetPart’].Position” or when i did “if v[‘TargetPart’]:IsDescendantOf(workspace.Enemies) then”, it just broke out of the loop, not erroring at all.