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

way to get mouse.Hit.Changed?

Is there a way to fire a function when the local player's mouse.Hit changes? The above does not work, nor does

mouse.Changed:connect(function(prop)
	if prop == "Hit" then
		print "Test"
	end
end)

EDIT: I'm looking at the wiki's page on the changing of an instance's property, and it doesn't seem to work for game.Players.LocalPlayer:GetMouse()

2 answers

✓ Accepted answer

I just did some quick testing and it doesn't seem like the Mouse object will fire the Changed event at all. However, since you just want to know when Mouse.Hit changes then you should just be using the Mouse.Move event. I can't think of any scenario where Hit would change without that event being fired.

4 comments

0

If the Player moves without the Mouse being touched.

0

If the part the player is looking at moves or the player moves without touching the mouse then it won't change

0

Thanks for the input, I should have thought this through more.

0

Couldn't you just check if the X or Y property is different when the mouse moves instead of using Hit?

Normally, I would advise against busy loops, however in this case there simply is no alternative to a busy loop because I don't think Camera.Changed fires for attach. If I'm wrong, tell me and I'll adjust the script to get rid of the busy loop.

local ChangedEvent = Instance.new("BindableEvent");
local lastHit;
local Mouse = game.Players.LocalPlayer:GetMouse();
Mouse.Moved:connect(function()
if Mouse.Hit ~= lastHit then
	lastHit = Mouse.Hit;
	ChangedEvent:Fire(lastHit);
end;
end);
while wait(0.08) do
	if Mouse.Hit ~= lastHit then
		lastHit = Mouse.Hit;
		ChangedEvent:Fire(lastHit);
	end;
end;

How to use:
To get the event for the Hit changing, use ChangedEvent.Event:connect()

How does it work?
We look for the Mouse moving, or just occasionally checking the mouse hit, and comparing it to the hit on the previous comparison. If it is different, we change the value for the last comparison and fire the fake Changed event.

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