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

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

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

✓ Accepted answer

"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

2 comments

0

What's the diffrence between a BasePart and a Part?

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

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