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

Make a clickbutton that opens only on a left click?

Asked by
Klamman 220 Moderation Voter
8 years ago

I was looking at some code that I wrote in the past, and it never occurred to me before that the button that it belongs to can be opened using either the left or right mouse button. Here's the code

function BarrierBreak()
    game.Workspace.Barrier:Destroy()
    game.Workspace.BarrierBreaker:Destroy()
end

script.Parent.ClickDetector.MouseClick:connect(BarrierBreak)

How can I make the button only run the function on a left click?

1
I'm reasonably sure you would have to make your own 'clickdetector' using Mouse.Target and Mouse.Button1Down BlackJPI 2658 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

You'd probably want to make a local script that handles the interaction, like the comment mentioned. It could look something like this:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local barrier = game.Workspace.PathToBarrier

function MouseClick()
    local target = mouse.Target
    if target == barrier then
        target:Destroy()
        target:Destroy()
    end
end

mouse.Button1Up:connect( MouseClick )

Now, instead of statically defining the barrier, you'd probably want to check it by it's name, or have some special value placed into the barrier, that would help the script recognize if it's a barrier.

Ad

Answer this question