Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

"Instant" welding script issue, throws an error on Line 58?

Asked by 5 years ago
Edited 5 years ago

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.

0
So which line is 58? danglt 185 — 5y
0
yea, don't paste a 33 line script when you have an error on a line after 33 theking48989987 2147 — 5y
0
@PrismaticScripts I've put screenshot of Line 58 at the top, before putting the script. Ryoshote 0 — 5y
0
Are you trying to make it so if the player tries to unequip the tool, it will be re-welded to the player? SerpentineKing 3885 — 5y
View all comments (6 more)
0
@theking48989987 What does that have to do with anything? It is only a snippet, it isn't the full script itself. But it is a snippet of where I believe the error is occurring. Ryoshote 0 — 5y
0
@SerpentineKing Not really, no - I know it looks silly on the inside. But it works perfectly on the outside, except for this occurring error. Ryoshote 0 — 5y
0
what did you define parts as? theking48989987 2147 — 5y
0
and, instead of using text for links, actually embed the link of the image to the text theking48989987 2147 — 5y
0
@theking48989987 local Parts = Tool:GetChildren() Ryoshote 0 — 5y
0
im not sure why you set v1 equivalent to Parts when you could just iterate through parts theking48989987 2147 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Generic For Loops


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

Application


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


Original Errors


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

References


generic for loops

Hopefully this helped with your problem!


if there is anything i missed, just leave a comment

0
Seems to work - but this is now the problem http://prntscr.com/mee1tr, and I appreciate your help and understanding! Ryoshote 0 — 5y
0
if you had two pieces as a handle, you would need to weld them together, or just have one union / mesh / part as the handle. theking48989987 2147 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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
0
I've attempted to do what you have suggested, but it now seems to be returning this following error: http://prntscr.com/medi7c Ryoshote 0 — 5y
0
0
http://prntscr.com/medmk0 - It throws this error, I assume you're talking about this line: local v2 = 1, #v1 do Ryoshote 0 — 5y
0
yes, thats what i mean, i adjusted the script, try that SerpentineKing 3885 — 5y
View all comments (4 more)
0
It has got rid of the errors, but the trouble is it sorta works, I mean it welds but not to the handle correctly. It gives me this: http://prntscr.com/medpzq Ryoshote 0 — 5y
0
http://prntscr.com/medr9v - Done as shown Ryoshote 0 — 5y
0
I need to see the rest of the script in order to determine the welding properties, since if the function doesn't error then I've fixed the problems with this portion of the code. If needed, you can DM me the full script on discord here: SerpentineKing#0155 SerpentineKing 3885 — 5y
0
Added. Thank you. Ryoshote 0 — 5y

Answer this question