Script not recognizing mouse.Target

In my script I have it check if mouse.Target is equal to a certain name but it does not recognize it. I keep getting an error

Script (Is a local script)

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()

Mouse.Move:Connect(function()
	if Mouse.Target ~= nil and Mouse.Hit.p ~= nil then
		if Mouse.Target ~= 'Baseplate' or 'OverheadPanel' or 'Part' and Mouse.Target.On then
			script.Parent.Parent.Enabled = true
			script.Parent.Text = tostring(Mouse.Target).. " : ".. tostring(Mouse.Target.On.Value)
			wait(0.01)
		end
	elseif Mouse.Target == 'Baseplate' or 'OverheadPanel' or 'Part' then
		script.Parent.Parent.Enabled = false
	else
	
		script.Parent.Parent.Enabled = false
	end
end)


The ScreenGui is still enabled after I hover over the Baseplate

what is the error you get? post a screenshot of output if you can, or just send the error

On is not a valid member of Part “Workspace.Baseplate”

can you post a screenshot please?

so the error is in this line

script.Parent.Text = tostring(Mouse.Target).. " : ".. tostring(Mouse.Target.On.Value)

You’re setting the mouse.Target on value, but there is no On inside the baseplate. whatever you’re supposed to be setting On to, change it to that

I have this line though doesnt it check if mouse.Target is equal to Baseplate

it won’t go any further than the “on” part if there’s an error, so fix that error

I am setting mouse.target on to the part that its hovering, there are multiple parts with “on” in it

then try something like this
if Mouse.Target:FindFirstChild(“On”) then
Mouse.Target.On.Value

This whole line is a mess.
This is how Lua interprets this line

if Mouse.Target ~= "Baseplate" or "OverheadPanel" ~= nil or ("Part" ~= nil and Mouse.Target.On) then

So for the first part. Mouse.Target will never be “Baseplate” because “Baseplate” is a string and mouse.Target is an object.
if Mouse.Target ~= workspace.Baseplate then
For the second part, OverheadPanel is not nil and therefore will always be true. This if statement will always succeed.
if Mouse.Target ~= workspace.Baseplate and Mouse.Target.Name ~= "OverheadPanel" then
For the last part, and has a higher priority than or. We’ve fixed that by removing or altogether though, so you don’t need to worry about that. What you do need to worry about is that Mouse.Target.On will error if On doesn’t exist, so you need to use FindFirstChild.

if Mouse.Target ~= workspace.Baseplate and Mouse.Target.Name ~= "OverheadPanel" and Mouse.Target.Name ~= "Part" and Mouse.Target:FindFirstChild("On") then

Then apply this to your second if statement and you should be good on that front.

1 Like