I'm getting this annoying error, I can't seem to work out where I have gone wrong - perhaps someone could point me in the right direction? (There is more functions, etc to this script but the error doesn't come from any other function calls).
Screenshots: http://prntscr.com/med2mi - Note that this is a LocalScript within a Tool in StarterPack. http://prntscr.com/med35d - Error in the output I keep receiving. http://prntscr.com/med4aq - Line 58 itself. >:O
Note: I've tried doing this: http://prntscr.com/med5l1 - But then it seems to throw this error: http://prntscr.com/med635 (Seems to appear when the .Equipped and .Unequipped events fire)
local function InstantWeld() CurrentWelds = {} -- Empty the table GetObjects(Handle) coroutine.resume(coroutine.create(function() wait(0.1) for Parts = 1, Handle:GetChildren() do -- Line 58 local v1 = Parts local v2 = 1, #v1 do if v1[v2]:IsA("Weld") then local Break = false for v3 = 1, #CurrentWelds do if v1[v2] == CurrentWelds[v3] then Break = true end end if not Break then v1[v2]:Destroy() end end end end end)) end Tool.Equipped:connect(function() InstantWeld() end) Tool.Unequipped:connect(function() InstantWeld() end) InstantWeld()
Thank you for your time, warm regards, Ryoshote.
There is a way you can avoid this problem entirely, by switching from numeric for loops to generic for loops. A numeric for loops would be the most common type of for loop, found in almost every language.
Ex:
for iter = start, finish, increment do ... end
However, lua has a more intuitive way to handle your current situation, a generic for loop, which can be formatted as such:
for index, value in pairs(table) do ... end
Using the generic for loop, you can easily shorten the process of iterating through tables from
for i = 1, #table do if table[i] == "something" then ... end end
to
for i,v in pairs(table) do if v == "something" then ... end end
This won't just shorten your script, it solve your current problem, which all relate to the length operator and the declaration of for loops.
Note: you could use the spawn() function instead of resuming a created coroutine
local function InstantWeld() CurrentWelds = {} -- Empty the table GetObjects(Handle) spawn(function() wait() for a_,Val1 in pairs(Handle:GetChildren()) do local v1 = Parts for _,Val2 in pairs(v1) do if d:IsA("Weld") then local Break = false for _, Val3 in pairs(CurrentWelds) do if Val2 == Val3 then Break = true end end if not Break then Val2:Destroy() end end end end end)) end Tool.Equipped:connect(function() InstantWeld() end) Tool.Unequipped:connect(function() InstantWeld() end) InstantWeld()
Note: I am not fully aware of what you are trying to accomplish, so I just ended up modifying your original script :p
1: Declaring a for loops with local
instead of for
\
local v2 = 1, #v1 do
Fixed Version :
for v2 = 1, #v1 do end
2: using a table instead of a number as the finish point of a numeric for loop
for Parts = 1, Handle:GetChildren() do
FixedVersion`
for Parts = 1, #Handle:GetChildren() do
Hopefully this helped with your problem!
if there is anything i missed, just leave a comment
I think the issue, based on the error, is this line:
for Parts = 1, Handle:GetChildren() do
When you call Handle: GetChildren()
you are asking for a Table, not a number, so you need to place a Hashtag in front of it like:
for Parts = 1, #(Handle:GetChildren()) do
I'm not sure if you need the extra parentheses, if you dont, remove them, but just in case this should work:
local handle = Handle:GetChildren() for Parts = 1, #handle do
Please note this is simply based on the amount of the script I have access to, so I can't tell if this is the true cause of the error (there is no line 58 in the Question)
EDIT: I just looked at your other attempt, which functioned similar to this, but I can't tell why you tried to get the # of #Part, since #Part was already a number to begin with
Regarding Comment #1
Try this, since you already have a # from the handle, a second iteration is not required
local handle = Handle:GetChildren() for i = 1, #handle do if handle[i]:IsA("Weld") then local Break = false for v3 = 1, #CurrentWelds do if handle[i] == CurrentWelds[v3] then Break = true end end if not Break then handle[i]:Destroy() end end end