Hello. I’m working on a system in which the mouse will hover items with a ClickDetector inside, a UI element will appear on screen. But as I was testing, the UI element appears only for a brief moment and not when my mouse leaves the object. Would appreciate any help.
Your code looks like it should work.
Have you looked at what your code is doing? Maybe you could add some print statements to debug it.
For example, I would first verify that the mouse.Target is what you expect it to be (e.g. by printing out the name of what the target is every 0.1 seconds), then I would make sure that the logic is correct, and finally, if there are no problems with the other things, that the UI is being shown properly.
For clarification, the UI does disappear when the mouse leaves the object? And for the appearing, it appears momentarily the first time the mouse enters the object? Or does it flip between being visible and invisible?
Okay, I did some testing with print statements:
When testing, I noticed that nothing was printing prior to me actually being in range of the object I wanted to interact with. It was only printing when my mouse entered/left the object. Secondly, I have also noticed that the UI only stays when my character is physically moving? I’m not exactly sure what to make of that. If needed, I can screen record and post it. And to clarify, yes. The UI only appears for only a moment when my mouse first hovers over the object, and does not stay even when my mouse is still hovering when my character is not in motion.
I tested this in studio and it works fine. Could you check out this place:
MouseTargetTest.rbxl (55.1 KB)
Which uses this code
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
while task.wait(0.1) do
print(mouse.Target)
if mouse.Target and mouse.Target:FindFirstChildWhichIsA("ClickDetector") and mouse.Target.Name ~= "ItemDetector" then
game.Lighting.Brightness = 4
else
game.Lighting.Brightness = 3
end
end
on some example parts.
I’m not sure what your problem is. I would recommend checking what the mouse.Target is that’s causing it to not register. Maybe you have some invisible parts or something it’s hitting instead of the target.
I would also add a condition for mouse.Target
and
ed before your other conditions to make sure the target exists before using the variable (as seen in the code above). Otherwise it should error out I think.