Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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.

0
"Moved is not a valid member of Mouse" That's the error I am getting DarkDanny04 407 — 4y
0
I fixed it. youtubemasterWOW 2741 — 4y
Ad

Answer this question