Unable to cast value to Object

Hi!
Simple issue, when trying to get IsDecendantOf(), I get the error in the title.
image
Code:

--function:
local function StartDel(v)
	if not v.Name == "Grid" and v:IsDecendantOf(BuildPlate) then
		v:Destroy()
	end
end
--later in the code, probably like 200 lines down
script.Parent.Parent.Parent.ControlBar.Delete.MouseButton1Click:Connect(function()
	Mouse.Move:Connect(function()
		if not Player.PlayerGui:FindFirstChild("BuildSelection") then
			local BuildSelection = Instance.new("SelectionBox")
			BuildSelection.Parent = Player.PlayerGui
			BuildSelection.Color3 = Color3.fromRGB(255, 69, 72)
		end
		local Box = Player.PlayerGui:FindFirstChild("BuildSelection")
		if Mouse.Target:IsDescendantOf("BuildPlot") then
			Box.Adornee = Mouse.Target
			Mouse.Button1Up:Connect(function()
				StartDel(Mouse.Target)
			end)
		else
			Box.Adornee = nil
		end
	end)
end)

Thanks!

1 Like

This needs to be an instance, not a string.

2 Likes

image

1 Like

Box likely doesn’t exist since it’s the only thing you are changing Adornee of.

1 Like

I know, but I made it so it creates the box.

1 Like

You never set it’s name to ‘BuildSelection’.

2 Likes

Sorry, I keep making REALLY stupid mistakes, i wrote it in discord lol

1 Like

image

1 Like

This is an instance now, right?

1 Like

Yes, it now looks like this.

script.Parent.Parent.Parent.ControlBar.Delete.MouseButton1Click:Connect(function()
	Mouse.Move:Connect(function()
		if not Player.PlayerGui:FindFirstChild("BuildSelection") then
			local BuildSelection = Instance.new("SelectionBox")
			BuildSelection.Parent = Player.PlayerGui
			BuildSelection.Color3 = Color3.fromRGB(255, 69, 72)
			BuildSelection.Name = "BuildSelection"
		end
		local Box = Player.PlayerGui:FindFirstChild("BuildSelection")
		if Mouse.Target:IsDescendantOf(BuildPlate) then
			Box.Adornee = Mouse.Target
			Mouse.Button1Up:Connect(function()
				StartDel(Mouse.Target)
			end)
		else
			Box.Adornee = nil
		end
	end)
end)
1 Like

Mouse.Target is nil, you should do:

if Mouse.Target and Mouse.Target:IsDescendantOf(BuildPlate) then

1 Like

It’s not getting to that because as you said the target is nil.

1 Like

Do this:

script.Parent.Parent.Parent.ControlBar.Delete.MouseButton1Click:Connect(function()
	Mouse.Move:Connect(function()
		if not Player.PlayerGui:FindFirstChild("BuildSelection") then
			local BuildSelection = Instance.new("SelectionBox")
			BuildSelection.Parent = Player.PlayerGui
			BuildSelection.Color3 = Color3.fromRGB(255, 69, 72)
			BuildSelection.Name = "BuildSelection"
		end
		local Box = Player.PlayerGui:FindFirstChild("BuildSelection")
		if Mouse.Target and Mouse.Target:IsDescendantOf(BuildPlate) then
			Box.Adornee = Mouse.Target
			Mouse.Button1Up:Connect(function()
				StartDel(Mouse.Target)
			end)
		else
			Box.Adornee = nil
		end
	end)
end)
1 Like

That’s the exact same code…?

1 Like

It has a check to see if Mouse.Target is nil

2 Likes

Nothing changed, and I swear that’s the same code.

1 Like

Then I can’t help, since I added a check to check if mouse.Target exists and you are saying it has the same error.

2 Likes

Check your message. All that’s new is this.

image
I do this at the start of my code.

1 Like

Setting the target filter does not make sure that mouse.Target is not nil

2 Likes

yes, but the mouse would be pointing at the brick making it’s target the model?

1 Like