How can I detect a change in Mouse.Target using GetPropertyChangedSignal?

What I Want To Achieve
I’m making a game that has the First 1000 ROBLOX Accounts. When a user hovers their mouse over the Character they want to see information about, a GUI is supposed to appear and tell them that information.

The Issue
The problem is that when I attempt to detect a change in the Target property of Mouse using Instance:GetPropertyChangedSignal, it does not fire whatsoever. Keep in mind that no errors occur in the script.

Solutions I’ve Tried
I’ve attempted to use Instance.Changed, however nothing fired when I tried that. On the Instance:GetPropertyChangedSignal page, it states ValueBase objects, such as IntValue and StringValue, use a modified Changed event that fires with the contents of the Value property. As such, this method provides a way to detect changes in other properties of those objects. This made no sense to me, as I tried doing what it said but was lost.

Script:

local Mouse = game.Players.LocalPlayer:GetMouse()
local Camera = workspace.CurrentCamera

local InfoFrame = game.Players.LocalPlayer.PlayerGui.InfoGui.InfoFrame

local function onChanged(Property)
	local Target = Mouse.Target
	if Property == "Target" then
		if Target ~= nil then
			if Target.Name == "StatueBase" then
				InfoFrame.Position = UDim2.new(0, Mouse.X, 0, Mouse.Y)
				local Name = Target:FindFirstChildOfClass("StringValue")
				local UserId = Target:FindFirstChildOfClass("NumberValue")
				if Name.Value == "" then
					InfoFrame.Info1.Text = "[ Doesn't Exist ]"
					InfoFrame.Info2.Text = "nil"
				else
					InfoFrame.Info1.Text = Name.Value
					InfoFrame.Info2.Text = UserId.Value
				end
			end
		end
	end
end

Mouse.Changed:Connect(onChanged)

The location of the LocalScript is game.StarterPlayer.StarterPlayerScripts.

Any help would be great. Thank you.

This post goes into detail on your issue and has the solution I think you are looking for.

1 Like

Thank you. It worked! I’m not sure why they would include Changed function of Mouse if it doesn’t work properly…

If there’s some reason you specifically need a changed function:

--> Module
local Targeting, IO = { }, game:GetService("UserInputService")

function Targeting.new(Player, Camera)
	local this = { }
	this.target  = nil
	this.event   = Instance.new "BindableEvent"
	this.Changed = this.event.Event
	
	local function getTarget()
		local pos = IO:GetMouseLocation()
		return game.Workspace:FindPartOnRay(
			Ray.new(Camera.CFrame.p, Camera:ViewportPointToRay(pos.x, pos.y, 0).Direction * 1000)
		)
	end
	
	IO.InputChanged:connect(function ()
		local upd = getTarget()
		if upd ~= this.target then
			this.target = upd
			this.event:Fire(this.target)
		end
	end)
	
	return this
end

return Targeting

In a localscript:

--> LocalScript
local Player = game.Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera

local Targeting = require(game:GetService("ReplicatedStorage").TargetingModule).new(Player, Camera)
Targeting.Changed:connect(function (obj)
	print("New object selected: ", obj)
end)
2 Likes