Hey, I'm making an FPS and I've gotten rid of the default backpack in favour of weapons slots corresponding to keys on the Number Line. My Idea is that if you press 1 you select your primary and if you press 2 you select your sidearm; alternatively, if you press said key with the weapon already selected you can unequip to move faster.
Unfortunately, my script breaks upon two "if" statements
if inputObject.KeyCode == Enum.KeyCode.One then ---CODE for PRIMARY elseif inputObject.KeyCode == Enum.KeyCode.Two then --CODE for SIDEARM else ---Attempt at nil debounce end
and
if player.ClassInfo.ClassValue.Value == 1 then ---EQUIP PRIMARY else ---Attempt at nil debounce end
If anyone can help me I would really appreciate it, I'm new to lua but I understanding programming reasonably well so I know it works if I only have one Input but I would rather use one consilidated system for weapon selection. Thanks
Full Code Here
-- Make variables for the local player and ContextActionService local player = game.Players.LocalPlayer equippedPri = true equippedSec = false function onKeyPress(inputObject, gameProcessedEvent) --PRIMARY if inputObject.KeyCode == Enum.KeyCode.One then if player.ClassInfo.ClassValue.Value == 1 then if game.Players.LocalPlayer.Team == "BLUFOR" then if equippedPri == false then player.Character.Humanoid:UnequipTools() local gun = player.Backpack["M32 Carbine w/ Holo Sight"] player.Character.Humanoid:EquipTool(gun) equippedPri = true equippedSec = false elseif equippedPri == true then local gun = player.Character["M32 Carbine w/ Holo Sight"] player.Character.Humanoid:UnequipTools() equippedPri = false end end else --Attempt at nil debounce end --SECONDARY elseif inputObject.KeyCode == Enum.KeyCode.Two then if game.Players.LocalPlayer.Team == "BLUFOR" then if equippedSec == false then player.Character.Humanoid:UnequipTools() local gun = player.Backpack["P83"] player.Character.Humanoid:EquipTool(gun) equippedPri = false equippedSec = true elseif equippedSec == true then local gun = player.Character["P83"] player.Character.Humanoid:UnequipTools() equippedSec = false end end else --Attempt at nil debounce end end game:GetService("UserInputService").InputBegan:connect(onKeyPress)
I managed to fix it myself, this will limit the player to two weapons but it will work no matter what weapons the player has.
-- WEAPON VARIABLES equippedPri = true equippedSec = false function onKeyPress(inputObject, gameProcessedEvent) --PRIMARY if inputObject.KeyCode == Enum.KeyCode.One then if equippedPri == false then -- if Secondary equipped local primary = game.Players.LocalPlayer.Backpack local gun1 = primary:FindFirstChildOfClass("Tool") game.Players.LocalPlayer.Character.Humanoid:UnequipTools() game.Players.LocalPlayer.Character.Humanoid:EquipTool(gun1) equippedPri = true equippedSec = false elseif equippedPri == true then -- redundancy print("AlreadyEquipped") end end --SECONDARY if inputObject.KeyCode == Enum.KeyCode.Two then if equippedSec == false then -- if Primary equipped local secondary = game.Players.LocalPlayer.Backpack local gun = secondary:FindFirstChildOfClass("Tool") game.Players.LocalPlayer.Character.Humanoid:UnequipTools() game.Players.LocalPlayer.Character.Humanoid:EquipTool(gun) equippedPri = false equippedSec = true elseif equippedSec == true then -- redundancy print("AlreadyEquipped") end end end game:GetService("UserInputService").InputBegan:connect(onKeyPress)