Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

2 comments

0

root is probably not a Part. The variable a which is Part0 needs to be fed a Part.

1 answer

✓ Accepted 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.

1 comment

0

Works. I was doing some className work wrong, haha.

This discussion is preserved as it was. New answers, comments, and votes are closed.