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

why wont mouse.Changed fire?

local mouse = game:GetService('Players').LocalPlayer:GetMouse()

mouse.Changed:connect(function(x)
	print(".")
	if x == "Target" then
		print("NewTarget")
	end
end)

the mouse.Changed event wont fire and doesnt even print any "."

does it have anything to do with me using a local script? help

4 comments

0

this isn't answering your question, but :connect() is depreciated. use :Connect() instead. Plus, I think I heard the Mouse function is considered a legacy function and should not be used for new works

0

^^^ There is no "Changed" event. And as mentioned above, Mouse is considered legacy so you might want to use UserInputService or ContextActionService instead.

0

there is a changed event! check the wiki: https://developer.roblox.com/api-reference/class/Mouse

0

"Fired immediately after a PROPERTY of an object changes." In other words, "Changed" is not for USER INPUT. Nowhere in the script are you changing mouse properties.

2 answers

I did a bit of testing, and I came up with the following code. I use the ".Move" event instead of ".Changed" because it seems to work better, and it checks if the part the mouse is pointing at an object. If so, it checks the part's name, and if the part's name is "Target" it prints "NewTarget".

local mouse = game:GetService('Players').LocalPlayer:GetMouse()

mouse.Move:Connect(function(x)
if mouse.Target then
	if mouse.Target.Name == "Target" then
		repeat
			print("NewTarget")
			wait()
		until mouse.Target.Name ~= "Target"
	end
end
end)

if this helped, don't forget to accept this answer :D

2 comments

0

i actually was using the mouse.Move event earlier, the problem was that everytime a new block appeared in front of my mouse, the event wouldnt fire unless i moved my mouse again.

0

^ I fixed it so that it repeats as long as the mouse is on the object

Local Script

local mouse = game:GetService('Players').LocalPlayer:GetMouse()

mouse.Move:Connect(function()
	if mouse.Target and mouse.Target.Name == "Target" then
		print("NewTarget")
	end
end)

connect is considered deprecated (even though it sometimes works) use Connect instead

There are no parameters for the mouse.Move event

While the Changed event is valid for the Mouse, it will not work for this script, which is why we use the mouse.Move event instead.

Check that "mouse.Target" is not nil before using the function (this occurs when pointing it at the sky) otherwise, it will error and break.

EDIT

Based on your above comment, you could try adding this additional function

workspace.ChildAdded:Connect(function()
	if mouse.Target and mouse.Target.Name == "Target" then
		print("NewTarget")
	end
end)

The ChildAdded event should be able to check if the part is different, unless of course the part was already in the workspace?

2 comments

0

can i atleast know why the changed event wont work in this script?

0

according to the roblox wiki it should work

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