My script is supposed to equip a flashlight every time I hit "f", and remove the flashlight if it is already equipped. It's not working. Can anyone help?
01 | BP = script.Parent -- LocalScript is placed in the player's Backpack. |
02 | FL = BP.Flashlight |
03 | char = BP.Parent.Character -- Finds character. |
04 |
05 | Mouse = game.Players.LocalPlayer:GetMouse() |
06 |
07 | Mouse.KeyDown:connect( function (key) |
08 | local a = FL:clone() |
09 | if key = = "f" and a.Parent = = char then |
10 | a:remove() |
11 | else |
12 | a.Parent = char |
13 | end |
14 | end ) |
You are checking if "a" is in the Player, while you have set a to clone the Flashlight. You should rather check if the Flashlight is in the player. I'm not sure if this will make a difference, but might as well try.
ALSO: You are only activating the function if the F key is pressed AND the Flashlight is in the character. If the Flashlight isn't there, the function isn't activating. This is what I suspect is the problem.
01 | BP = script.Parent -- LocalScript is placed in the player's Backpack. |
02 | FL = BP.Flashlight |
03 | char = BP.Parent.Character -- Finds character. |
04 |
05 | Mouse = game.Players.LocalPlayer:GetMouse() |
06 |
07 | Mouse.KeyDown:connect( function (key) |
08 | local a = FL:clone() |
09 | if key = = "f" then |
10 | if char:FindFirstChild( "Flashlight" ) then |
11 | char:FindFirstChild( "Flashlight" ):Destroy() --remove is deprecated, use destroy. |
12 | else |
13 | a.Parent = char |
14 | end |
15 | end |
16 | end ) |
This should work, if not, let me know, and let me know if you have any errors in the output.