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

Script won't stop adding trees - how to stop?

Asked by 5 years ago

So this code is a plugin, which adds trees at the mouse's location. When I click to stop, it does not work. Here is the code:

01-- Create a new toolbar section titled "Custom Script Tools"
02open =  false
03local player = game.Players.LocalPlayer
04local mouse = player:GetMouse()
05local toolbar = plugin:CreateToolbar("Custom Script Tools")
06local ChangeHistoryService = game:GetService("ChangeHistoryService")
07 
08 
09-- Add a toolbar button named "Create Empty Script"
10local newScriptButton = toolbar:CreateButton("Create Snow Tree", "Create a Snow Tree", "rbxassetid://4458901886")
11 
12local function onNewScriptButtonClicked()
13    if open == false then
14        open = true
15        print("Awesomemode14 Plugin is Open")
View all 32 lines...

Thanks for the Help!

0
I do believe there is no "player" object in studio. And if I'm not mistaken(which I might be) you can just use plugin:GetMouse(), namespace25 594 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

For this problem you should familiarize yourself with Events, like for instance the Button1Down mouse Event which fires a signal every time the mouse button is pressed down.

Your current issue is that you are binding the mouse click to your function Button1Down:Connect(function() but then have no way to disconnect this event afterwards. What's worse, you try to bind a new function to this event, creating an ever increasing memory leak.

What you should do, is store the event you are binding as a variable - which can be done like so:

1local treeEvent = mouse.Button1Down:Connect(function()
2    --stuff
3end)

This is possible because the :Connect function returns the event it creates!

So now that we have the event stored in a variable, we can call the :Disconnect() function whenever we want to stop the game from listening for your mouse click.

This means replacing:

1mouse.Button1Up:Connect(function()end)

With:

1treeEvent:Disconnect()

Hope this helps!

0
How would I call the variable? awesomemode14 68 — 5y
0
I'm not sure what you mean by call the variable. It's just a reference to the event you created. If you mean rebinding the event, you have to use :Connect() again. FrogNinjaX 222 — 5y
Ad

Answer this question