Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

2 answers

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

8 comments

0

that's for something that I don't need anymore,I can remove it

0

Okay and also I tried the pcall one and it said it couldn't find "FindFirstChild".

0

Ah-ha that is funny I looked over the fact that you use "findFirstChild" use "FindFirstChild"

0

I am using "FindFirstChild"

0

Ah then that might be roblox being stupid again try putting Game:GetService"Players".LocalPlayer.CharacterAdded:wait() at the top of the script

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

0

Thanks

1

No problem! Feel free to ask any other questions here @ scripting helpers! :D

Okay so how would I make one for people who have vip t-shirts?

1 comment

0

I'm also experiencing a problem where I can keep hitting 1 and I will keep getting swords same with keep hitting 2.

This discussion is preserved as it was. New answers, comments, and votes are closed.