How to use the MouseEnter function continuously?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to check every second if the player is hovering over a GUI element
  2. What is the issue? Include screenshots / videos if possible!
    As for now, the “MouseEnter” function works if you hover over something, although it only runs once, then you have to move your mouse out of the GUI element, and then bring it back for it to work again.

But instead i would like to make it so that i dont need to change my mouse position again and again for it to work, instead i wont it to check if it the player is hovering over the GUI element every second.
3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried to put it in a while true statement and googled some stuff, but nothing works

local player = game.Players.LocalPlayer
local char = player.Character
local RightHovering = script.RightHovering.Value
local LeftHovering = script.LeftHovering.Value
-- I made this script, so that the players position changes whenever they hover over the label.

script.Parent.RightLabel.MouseEnter:Connect(function(X, Y)
	char.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.Position.X +0.1,char.HumanoidRootPart.Position.Y, char.HumanoidRootPart.Position.Z)
end)

3 Likes

This doesnt run every second since its inside the mouse enter function :confused:
This runs a lot of times , when i first put my mouse in the button, then it stops again.
And it makes me have to put my mouse back into the button for it to work again

Maybe you can try using a Repeat Loop like this,

script.Parent.RightLabel.MouseEnter:Connect(function(X, Y)
    repeat
 	  char.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.Position.X +0.1,char.HumanoidRootPart.Position.Y, char.HumanoidRootPart.Position.Z)
      wait(1)
    until script.Parent.RightLabel.MouseLeave
end)
1 Like

You should use the Mouse.Move event so the function runs with your mouse in real time, but because you want it to only work when your mouse is inside, you should make a bool, and add a MouseLeave function + MouseEnter function (you already have one so just edit it) to edit the bool to true or false.

local player = game.Players.LocalPlayer
local char = player.Character
local RightHovering = script.RightHovering.Value
local LeftHovering = script.LeftHovering.Value
Mouse = player:GetMouse()
Entered = false 
-- I made this script, so that the players position changes whenever they hover over the label.

script.Parent.RightLabel.MouseEnter:Connect(function(X, Y)
    Entered = true
	--I moved your line to the MouseMove since I think that's what you want when you said you wanted it to run continously.
end)

script.Parent.RightLabel.MouseLeave:Connect(function()
    Entered = false
    --You can add anything you want to happen when you leave your mouse.
end)

Mouse.Move:Connect(function()
     if Entered then
        char.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.Position.X +0.1,char.HumanoidRootPart.Position.Y, char.HumanoidRootPart.Position.Z)
     end
end)

That should work, let me know if something is wrong. :slightly_smiling_face:

This gives me an idea, what if i use a while true do loop, something like this:

while true do
if Entered == true then
-- change position
end
wait()
end

Do you think this would work?

It’s better to use Mouse.Move since it only updates when your mouse moves, if you update it repetitively with no delay that would possibly crash your game.

1 Like