Unable to cast value to object

I got an unable to cast value to object error in my code and I cannot seem to figure out what the issue is.

The error pointed to line 23 which is if Target:IsDescendantOf(Vehicle) then

Target is of course the PlayerMouse’s Target

function GetModel(Target)
	local Model
	for _, Vehicle in pairs, workspace.Vehicles:GetChildren() do
		if Target:IsDescendantOf(Vehicle) then
			Model = Vehicle
		end
	end
	if not Model then
		Model = Target.Parent
	end
	return Model
end

and the line where this function fired was local Debris = GetModel(Target) or Target

function MouseMove(Mouse)
	if IsAdministrator() and Enabled then
		local Target = Mouse.Target
		if Target then
			if IsDebris(Target) then
				local Debris = GetModel(Target) or Target
				Selection.Parent = workspace.Terrain
				Selection.Adornee = Debris
			else
				Selection.Adornee = nil
			end
		else
			Selection.Adornee = nil
		end
	end
end
3 Likes

Could you change that line to if Target and Target.Parent and Vehicle and Target:IsDescendantOf(Vehicle) then

i already check to see if target is not nil, i also know that vehicle exists too because its inside workspace as well as target’s parent

do print(Target, Target.Parent)
say if it says either is nil

The function only fires if both already exist, please look at the code first.

i am aware of what you said, but if you are getting that error, you have to check if it exists.

I know it exists because not only have I already checked, but the function where the error occurs inside of can only fire if they exist.

ok, well you can figure it out then.
if i was going to fix it myself, i would check using print()

1 Like

This is your problem. Vehicle is the same as workspace.Vehicles:GetChildren(), and that’s a table, not an Instance. You need to call pairs as a function rather than just pass it as an argument.

1 Like

i didnt even notice that, lol you fixed it

I did not notice that I did this, but it worked. Thank you.