-
What do you want to achieve?
I want to unequip/equip the right tool once a certain key is pressed -
What is the issue?
The script stops working once it reaching the 4th if statement. The same goes for the same block of code in the elseif statement.
--- 4th if statement
if smg then
humanoid:UnequipTools(pistol)
wait()
humanoid:EquipTool(smg)
end
---- 4th if statement located in the elseif statement
if pistol then
humanoid:UnequipTools(smg)
wait()
humanoid:EquipTool(pistol)
end
-
What solutions have you tried so far?
I’ve rewritten the code a few times in different ways but it still wouldn’t work properly.
I tried to add returns in parts of the code but that didn’t help either.
Please note that the CoreGuiType.Backpack is set to false, and the script parent is, StartCharacterScripts which is located in StartPlayer.
--- Variables
local UserInputService = game:GetService("UserInputService")
local WeaponsFolder = workspace:WaitForChild("Weapons Folder")
local player = game:GetService("Players").LocalPlayer
--- Script Function //-UserInput Detection-// Unequipping tool(pistol) and then equipping tool(smg)
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed then
if input.KeyCode == Enum.KeyCode.One then
if player and player.Character then
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
wait()
if humanoid then
local smg = WeaponsFolder:WaitForChild("SubmachineGuns"):FindFirstChild("SMG")
local pistol = WeaponsFolder:WaitForChild("HandGuns"):FindFirstChild("Pistol")
wait()
if smg then
humanoid:UnequipTools(pistol)
wait()
humanoid:EquipTool(smg)
end
end
end
--- Unequipping tool(smg) and then equipping tool(pistol)
elseif input.KeyCode == Enum.KeyCode.Two then
if player and player.Character then
local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
wait()
if humanoid then
local smg = WeaponsFolder:WaitForChild("SubmachineGuns"):FindFirstChild("SMG")
local pistol = WeaponsFolder:WaitForChild("HandGuns"):FindFirstChild("Pistol")
wait()
if pistol then
humanoid:UnequipTools(smg)
wait()
humanoid:EquipTool(pistol)
end
end
end
end
end
end)