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

Stop operation when tool is deselected?

Asked by
A9X 10
9 years ago
01Player = game.Players.LocalPlayer
02local tool = Instance.new("Tool")
03tool.Parent = Player.Backpack
04local mouse = Player:GetMouse()
05tool.Name = "SOMETHING"
06local tool2 = Instance.new("Part", tool)
07 
08tool2.Transparency = 1
09tool2.Name = "Handle"
10 
11tool.Selected:connect(function()
12    mouse.KeyDown:connect(function(key)
13        if key == "z" then
14            print("TESTING")
15        end
16    end)
17end)

How do I make it so when I un-equip the tool and then I press Z, nothing will happen?

1 answer

Log in to vote
0
Answered by 9 years ago

Every tool has these functions:

  • Equipped ( mouse object )
  • Unequipped

So, I would recommend making a function that you can connect/disconnect whenever the player equips/unequipped the tool, E.G:

1function printText()
2    print("Player joined the game.)
3end
4 
5local func = game.Players.PlayerAdded:connect(printText) -- does the function
6 
7func:disconnect() -- stops it from working again

So:

01Player = game.Players.LocalPlayer
02local tool = Instance.new("Tool")
03tool.Parent = Player.Backpack
04local mouse = Player:GetMouse()
05tool.Name = "SOMETHING"
06local tool2 = Instance.new("Part", tool)
07 
08tool2.Transparency = 1
09tool2.Name = "Handle"
10 
11function attack(key)
12    key = key:lower() -- very important, use this every time you check for a lowercase letter
13    if key == "z" then -- use tab for formatting, not spaces
14        print("TESTING")
15    end
View all 26 lines...

Hopefully this works. If not, just use a debounce.

0
Thank you, but can you also demonstrate how to use a debounce? I've asked around and people said its complicated to explain. A9X 10 — 9y
Ad

Answer this question