In my game I disabled the inventory on the bottom of the screen so I have to make a script to change tools. It works well but there's one problem and that is that you can press 1 or 2 over and over and you'll be holding both weapons and a bunch of them will clone in to the character.
Script (In a local script):
wait() local p = game.Players.LocalPlayer local h = p.Character.Humanoid local e = p:GetMouse() local down = false e.KeyDown:connect(function(key) if key:lower() == "1" and not db then down = true p.Backpack.LinkedSword:Clone().Parent = p.Character a = p.Character:findFirstChild("PaintballGun") a:Destroy() end end) e.KeyUp:connect(function(key) if key:lower() == "2"then down = true p.Backpack.PaintballGun:Clone().Parent = p.Character b = p.Character:findFirstChild("LinkedSword") b:Destroy() end end)
EDIT: After using Fattycat17's answer, I get this error message:
13:33:03.755 - FindFirstChlid is not a valid member of Model 13:33:03.758 - Script 'Players.Player1.Backpack.Equip', Line 12 13:33:03.760 - stack end 13:33:03.761 - Disconnected event because of exception
Oh god there are quite a few problems that I will automatically notice
b = p.Character:findFirstChild("LinkedSword") b:Destroy()
IS A BIG NO NO NO NO (This goes the same for the first line with)
a = p.Character:findFirstChild("PaintballGun")
This will error since if "LinkedSword" isn't actually there then it will return nil and we know that if you try to use the :Destroy() method on a nil value it returns an error
So I would either put a pcall around it or have an if statement
b=p.Character:FindFirstChlid("LinkedSword") pcall(function() b:destroy()end)
b=p.Character:FindFirstChlid("LinkedSword") if b then b:destroy() end
Second thing is... what exactly is "db" on line 9?
EDIT "FindFirstChlid is not a valid member of Model" ... "Chlid" ... "C H L I D" Ha-ha don't worry we all have those days filled with typos to clarify it's FindFirstChild!!