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

''Bad Cast'' Error being sent from modulescript, what am I doing wrong?

Asked by
drew1017 330 Moderation Voter
8 years ago

Haven't ever used MS's before, so cut me some slack if I'm doing this all wrong.

ServerScript:

local weld = jar.WeldTo(root, c, v.Joints, 'relative') -- None of these arguments are nil

ModuleScript(named 'jar')

local jar = {}

function jar.WeldTo(a, b, jfolder, cframe)
    local weld = Instance.new('Weld', jfolder)
    weld.Name = 'Weld'
    weld.Part0 = a -- Error: Bad Cast
    weld.Part1 = b
    if cframe == 'relative' then weld.C0 = a.CFrame:inverse() * b.CFrame
    else weld.C0 = cframe end
    return weld
end

return jar
0
root is probably not a Part. The variable a which is Part0 needs to be fed a Part. bosswalrus 84 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Try this:

local jar = {}

function jar.WeldTo(a, b, jfolder, cframe)
    if A:IsA("Part") and B:IsA("Part") then
        local weld = Instance.new('Weld', jfolder)
        weld.Name = 'Weld'
        weld.Part0 = a -- Error: Bad Cast
        weld.Part1 = b
        if cframe == 'relative' then weld.C0 = a.CFrame:inverse() * b.CFrame
        else weld.C0 = cframe end
       return weld]
    end
end

return jar

The reason why I added " if A:IsA("Part") and B:IsA("Part") then" is because if A or B is not a part, it will cause a cast error which means that you gave the script something that it is not looking for.

0
Works. I was doing some className work wrong, haha. drew1017 330 — 8y
Ad

Answer this question