Hola, I’m currently making a plugin that detectes where the mouse is, and when it clicks, which part is has clicked.
The script works fine, it highlight the parts i want but it never stop.
I would like to be able to enable it when i start the plugin, and disable it when i stop the plugin.
The script is the following;
plugin:Activate(false) -- gain non exclusive access to the mouse
local mouse = plugin:GetMouse()
--Variabili
local Before = Instance.new("Highlight")
local Modulo = require(script.Parent.Posizioni)
local function button1Down()
if mouse.Target.Name == "Casella" or mouse.Target.Name == "Spawner" then
if mouse.Target ~= Before then
Before:Destroy()
Before = Instance.new("Highlight")
Before.Parent = mouse.Target
Before.FillTransparency = 1
Modulo.Arrivo(Before)
end
end
end
mouse.Button1Down:Connect(button1Down)
Tried searching around but found nothing.
Thank you a lot!
Hello. Add a logical parameter to the function (it will be global for the script) and add the condition for executing the algorithm in this function if the parameter is true. Next, create a second function that will be triggered by pressing the UI button or in some other way. In it, you will specify the inversion of this logical parameter.
plugin:Activate(false) – gain non exclusive access to the mouse
local mouse = plugin:GetMouse()
–Variabili
local Before = Instance.new(“Highlight”)
local Modulo = require(script.Parent.Posizioni)
local MouseKey = script.Parent. MouseKey.Value
local function button1Down(MouseKey)
if (mouse.Target.Name == "Casella" or mouse.Target.Name == "Spawner") and MouseKey == true then
if mouse.Target ~= Before then
Before:Destroy()
Before = Instance.new("Highlight")
Before.Parent = mouse.Target
Before.FillTransparency = 1
Modulo.Arrivo(Before)
end
end
end
mouse.Button1Down:Connect(function ()
button1Down(MouseKey)
end
)
The second local script. Change the value using the F key. Create a boolean variable named MouseKey next to the local script.
local ContextActionService = game:GetService(“ContextActionService”)
local MouseKey = script.Parent. MouseKey.Value
local function handleAction(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
if actionName == “Key” then
MouseKey = !MouseKey
end
end
end
ContextActionService:BindAction(“Key”, handleAction, true, Enum.KeyCode.F)
As a side note to remove the use of bool values, when you activate the plugin call an initialize function, then a de-initialize function when you stop the plugin through a module script. You can do it by putting the event in a table as shown below.
local ConnectionTable = {}
return {
Initialize = function(mouse, button1Down)
ConnectionTable[mouse] = mouse.Button1Down:Connect(button1Down)
end,
Deinitialize = function(mouse)
if ConnectionTable[mouse] then
ConnectionTable[mouse]:Disconnect()
end
end,
-- OR if you dont send the variable
DeinitializeAll = function()
for Index, Connection in pairs(ConnectionTable) do
if ConnectionTable[Connection] then
ConnectionTable[Connection]:Disconnect()
end
end
end
}