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

How do I detect when the mouse target changes while holding down the mouse click?

This is the code I have

local Tool = script.Parent
local player = game.Players.LocalPlayer


Tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		if Mouse.Target == game.Workspace.EmeraldBlock.Part then
			player.PlayerGui.ScreenGui.Frame.Visible = true
			player.PlayerGui.ScreenGui.Frame.Frame1:TweenSize(UDim2.new(1.01, 0,0.63, 0), nil, "Linear", 5, false )
		end
	end)
	Mouse.Button1Up:Connect(function()
		if Mouse.Target ~= game.Workspace.EmeraldBlock.Part then
		player.PlayerGui.ScreenGui.Frame.Visible = false
		player.PlayerGui.ScreenGui.Frame.Frame1:TweenSize(UDim2.new(0, 0,.63,0), nil, nil, .01, true )
		end
	end)
end)

What happens is when I move my mouse away from the block while i'm holding down mouse button 1, the GUI continues to tween, even though the mouse target is no longer that part

1 answer

✓ Accepted answer

Hello. You have to use Mouse.Move with the :Disconnect() function. Try this:

local Tool = script.Parent
local player = game.Players.LocalPlayer

Tool.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        if Mouse.Target == game.Workspace.EmeraldBlock.Part then
            player.PlayerGui.ScreenGui.Frame.Visible = true
            player.PlayerGui.ScreenGui.Frame.Frame1:TweenSize(UDim2.new(1.01, 0,0.63, 0), nil, "Linear", 5, false )

	    local move

	    local function onMove()
                          player.PlayerGui.ScreenGui.Frame.Visible = false
      			  player.PlayerGui.ScreenGui.Frame.Frame1:TweenSize(UDim2.new(0, 0,.63,0), nil, nil, .01, true )
			move:Disconnect()
	    end

            move = Mouse.Move:Connect(onMove)
        end
    end)
    Mouse.Button1Up:Connect(function()
        if Mouse.Target ~= game.Workspace.EmeraldBlock.Part then
        player.PlayerGui.ScreenGui.Frame.Visible = false
        player.PlayerGui.ScreenGui.Frame.Frame1:TweenSize(UDim2.new(0, 0,.63,0), nil, nil, .01, true )
        end
    end)
end)

When the mouse moves, the Frame turns invisible and tweens the Frame's Frame1. Then it disconnects the function. Please upvote and accept this question if it helped.

2 comments

0

"Moved is not a valid member of Mouse" That's the error I am getting

0

I fixed it.

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