0

# Switching Tools

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
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: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

1
MunimR 125
7 years ago

Oh god there are quite a few problems that I will automatically notice

First

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

pcall method:

b=p.Character:FindFirstChlid("LinkedSword")
pcall(function() b:destroy()end)


if statement:

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!!

0
that's for something that I don't need anymore,I can remove it call23re 0 — 7y
0
Okay and also I tried the pcall one and it said it couldn't find "FindFirstChild". call23re 0 — 7y
0
Ah-ha that is funny I looked over the fact that you use "findFirstChild" use "FindFirstChild" MunimR 125 — 7y
0
I am using "FindFirstChild" call23re 0 — 7y
0
Ah then that might be roblox being stupid again try putting Game:GetService"Players".LocalPlayer.CharacterAdded:wait() at the top of the script MunimR 125 — 7y
0
"FindFirstChlid is not a valid member of Model" ... "Chlid" ... C H I L D Ha-ha don't worry we all have those days filled with typos MunimR 125 — 7y
0
Thanks call23re 0 — 7y
1
No problem! Feel free to ask any other questions here @ scripting helpers! :D MunimR 125 — 7y