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

Got a error saying "bad cast",how do I fix it?

Asked by 10 years ago

So I made this script and I keep the a error saying bad cast,what does it mean and how could I fix it? If you manged to fix it please show how and what you did.

Error:

bad cast

Script 'Workspace.Model.Script', Line 11

Stack End

--~-----------Vairbles---------------~--
local Model = script.Parent
---------------------------------------------   
    for Number,Part in pairs (Model:GetChildren()) do
--------------More Vairbles----------------
local PreviousPart = math.floor(Number - 1)
local PreviousPartName = "Part"..PreviousPart
local Motor = Instance.new("Motor6D",Part)
-------------------------------------------
    print(PreviousPartName)
    Motor.Part0 = Part
    Motor.Part1 = Model.PreviousPartName
    print(Motor.Part0)
    Motor.DesiredAngle = -Number
    end

The model is set up like this:

Model Setup Picture/Error

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

"Bad cast" happens when you give a ROBLOX function or object something which it's not looking for.

In this case, you are giving your script (called "Script" in explorer) to Part0, on line 11.

This is because in your loop you don't check that Part is actually a part!

You need to add a :IsA check to each Part to make sure:

local Model = script.Parent

for Number,Part in pairs (Model:GetChildren()) do
    if Part:IsA("BasePart") then
        local PreviousPart = math.floor(Number - 1)
        local PreviousPartName = "Part"..PreviousPart
        local Motor = Instance.new("Motor6D",Part)

        print(PreviousPartName)
        Motor.Part0 = Part
        Motor.Part1 = Model.PreviousPartName
        print(Motor.Part0)
        Motor.DesiredAngle = -Number
    end
end

0
What's the diffrence between a BasePart and a Part? kevinnight45 550 — 10y
0
"BasePart" is a 'super class' which means you can't actually make one. BUT, all part-like things are BaseParts (WedgePart,TrussPart, SpawnLocation, CornerWedgePart, etc) so it would catch anything you can touch BlueTaslem 18071 — 10y
Ad

Answer this question