✓ Accepted answer
You would and should use arguments for this o.o
There are different ways you can go about doing this but I am going to use classical ole if statements cause I love those, they're nice :D
Anyways...
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Tool = game.ReplicatedStorage['Pistol']
Mouse.KeyDown:connect(function(Key)
local NewKey = Key:lower()
if NewKey == 'q' then
if Player.Backpack:FindFirstChild('Pistol') == nil then
Tool:Clone().Parent = Player.Backpack
elseif Player.Backpack:FindFirstChild('Pistol') ~= nil then
Player.Backpack.Pistol:Destroy()
end
end
end)
Explanation:
The first if statement basically says if the Pistol is equal to nil (doesn't exist) then it'll put the tool (Pistol) in the players backpack. If the tool is not equal to nil('~=', is there) then it will destroy the Pistol.
Extra Info:
You should always use :Destroy() when removing / deleting something with a script so that way it doesn't cause "zombies" in your code and hog up memory, which could cause lag if you use rusty old :remove() in too many scripts.
This is one of the many ways you can do this but I think this is the quickest way.
I hope this works, if not let me know!
1 comment
I highly suggest using the UserInputService and looking at input began/ended. Mouse.KeyDown is outdated.