Script Stops Working Once It Reaches An If Statement

  1. What do you want to achieve?
    I want to unequip/equip the right tool once a certain key is pressed

  2. 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
  1. 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)

What is the parent of the script?

The parent of the script is, StarterCharacterScripts, which in located in StarterPlayer.

Well firstly

humanoid:UnequipTools(smg)

it should be humanoid:UnequipTools(). Also put

print("abc")

in the fourth if statement and tell me if it prints to the output.

1 Like

:UnequipTools has no return type, doing humanoid:UnequipTools(pistol) will return nil, you don’t have to return anything to the function, the function itself unequips all tools.

Use:
humanoid:UnequipTools()
instead of:
humanoid:UnequipTools(smg)

That’s a parameter, not a return, and it wouldn’t actually be the problem.

OP, you should use the debugger and see which line it stops on.

2 Likes

The script prints ABC but after the user has pressed key[1] or key[2] a numbers of times the script breaks and it never reaches the two if statements

--- 4th if statement
if smg then
   humanoid:UnequipTools()
   print("ABC"); wait()
   humanoid:EquipTool(smg)
end

--- 4th if statement located in the elseif statement
if pistol then
   humanoid:UnequipTools()
   print("ABC"); wait()
   humanoid:EquipTool(pistol)
end

For people wondering what the problem was. The line of code humanoid:EquipTool(smg) was setting the smgs parent to the player’s character which would cause the variable local smg = WeaponsFolder:WaitForChild("SubmachineGuns"):FindFirstChild("SMG") to turn into an infinite yield (that infinite yield warning never showed in the output). After that, the line of code humanoid:UnequipeTools() would set smgs parent to the player’s backpack. The script never goes into the player’s backpack to grab the smg which is why it wouldn’t work, same applies to the pistol.

1 Like

Instead of putting [SOLVED] in the title, could you mark your latest reply as the solution please?

1 Like