Best way for onhover

I don’t want to use a mousedetector, and I don’t think that a constant loop & or mouse move function would be the best. I’m wondering what the best way to detect if a user is hovering over an object and if they are a gui will pop up. This object must be certain one (btw)

2 Likes

By object, I believe you mean a BasePart?

Oh sorry. Yes, I was mainly thinking of an object like a gun or something but in reality it is a basepart.

If so, your best bet would be binding a function to Mouse.Move(). It shouldn’t impact performance in any major fashion.

Following on from what @Kiansjet said, you could do the following:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
-- Model containing baseparts we can mouse over
local HoverModel = workspace.HoverableObjects

Mouse.Move:Connect(function()
    local Object = Mouse.Target

    if Object:IsDescendantOf(HoverModel) then
        -- Do stuff, e.g. show a tooltip GUI
    end
end)
3 Likes

Don’t use PlayerMouse. Use a UserInputService alternative. PlayerMouse is probably going to become deprecated.

I’ve never worked with binding functions to mouse movements, but there should be an event to help you here:

http://wiki.roblox.com/index.php?title=API:Class/UserInputService

Hopefully someone has an explanation of how soon.

3 Likes

Don’t use something that works to solve this exact problem because it might become deprecated? That’s a bit daft.

4 Likes

Seems like pretty solid reasoning to me, seeing as a member of the Roblox staff recommends not using it.

4 Likes

http://wiki.roblox.com/index.php?title=API:Class/UserInputService/GetMouseLocation
Using RunService or something

http://wiki.roblox.com/index.php?title=API:Class/UserInputService/InputChanged
With the above method OR InputObject.Position (only the X and Y properties)
Don’t forget to check if the input type is UserInputType MouseMovement

The latter you could consider better but it doesn’t update when objects move

2 Likes

My open source StudioMouse module replicates PlayerMouse behavior using UserInputService. This would be ideal code to study for that purpose if you or OP are interested!

5 Likes

Oh, didn’t know about that!