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:
"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