"If" statement not working in code

I want to be able to remove an error from my code. I don’t know where it’s coming from because the “if” statement I made should stop it from occurring.

Here’s the code:

local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait() or Player.Character
local uis = game:GetService("UserInputService")
local Mouse = Player:GetMouse()
local f_Loop
local Camera = workspace.Camera
local follow = nil
folBool = false

uis.InputBegan:connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton3 then
        folBool = not folBool
        if folBool == true then    
            if Mouse.Target ~= nil then
                if Mouse.Target.Name == "Head" or "Right Arm" or "Left Arm" or "Right Leg" or "Left Leg" or "Torso" or "HumanoidRootPart" then --This should be stopping the bottom part from giving an error
                    print(Mouse.Target) --This prints even though the name isn't any of the strings listed above
                    local follow = Mouse.Target.Parent:FindFirstChild("Torso").Position --This gives an error; "Attempt to index nil with 'Position'"
                    f_Loop = game:GetService("RunService").RenderStepped:connect(function()
                        uis.MouseBehavior = Enum.MouseBehavior.LockCenter
                        Camera.CFrame = CFrame.new(Camera.CFrame.Position, follow)                      
                    end)
                elseif Mouse.Target.Name ~= "Head" or "Right Arm" or "Left Arm" or "Right Leg" or "Left Leg" or "Torso" or "HumanoidRootPart" then
                   print("No") --This doesn't print anything.
                    folBool = false
                end          
            end   
        end
        if folBool == false then
            f_Loop:Disconnect()
            print("Disconnected")
            uis.MouseBehavior = Enum.MouseBehavior.Default
        end
    end
    print(folBool)
end)

Screenshot 2020-12-22 161629
^This is when it printed Mouse.Target, even though the name wasn’t any of the strings. Then it later gave the error.

This problem is pretty unique so I couldn’t find anything online.

For further information, this code is used to lock on to any R6 character when the middle-mouse button is clicked.

2 Likes

try this???


local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait() or Player.Character
local uis = game:GetService("UserInputService")
local Mouse = Player:GetMouse()
local f_Loop
local Camera = workspace.Camera
local follow = nil
folBool = false

uis.InputBegan:connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton3 then
		folBool = not folBool
		if folBool == true then    
			if Mouse.Target ~= nil then
				if Mouse.Target.Name == "Head" or Mouse.Target.Name == "Right Arm" or Mouse.Target.Name == "Left Arm" or Mouse.Target.Name == "Right Leg" or Mouse.Target.Name == "Left Leg" or Mouse.Target.Name == "Torso" or Mouse.Target.Name == "HumanoidRootPart" then --This should be stopping the bottom part from giving an error
					print(Mouse.Target) --This prints even though the name isn't any of the strings listed above
					local follow = Mouse.Target.Parent:FindFirstChild("Torso").Position --This gives an error; "Attempt to index nil with 'Position'"
					f_Loop = game:GetService("RunService").RenderStepped:connect(function()
						uis.MouseBehavior = Enum.MouseBehavior.LockCenter
						Camera.CFrame = CFrame.new(Camera.CFrame.Position, follow)                      
					end)
				elseif Mouse.Target.Name ~= "Head" or  Mouse.Target.Name ~= "Right Arm" or  Mouse.Target.Name ~= "Left Arm" or  Mouse.Target.Name ~= "Right Leg" or  Mouse.Target.Name ~= "Left Leg" or  Mouse.Target.Name ~= "Torso" or  Mouse.Target.Name ~= "HumanoidRootPart" then
					print("No") --This doesn't print anything.
					folBool = false
				end          
			end   
		end
		if folBool == false then
			f_Loop:Disconnect()
			print("Disconnected")
			uis.MouseBehavior = Enum.MouseBehavior.Default
		end
	end
	print(folBool)
end)

Don’t worry, i made the same mistake of using “or” like that.

2 Likes

It worked but I don’t realise the difference. Can you tell me what you changed?

2 Likes

in your script

 if Mouse.Target.Name == "Head" or "Right Arm" or "Left Arm" or "Right Leg" or "Left Leg" or "Torso" or "HumanoidRootPart" then

Is wrong.
since you just gave it a string instead of telling the script to check what the Mouse.Target.Name is.
it then needs to be changed to

if Mouse.Target.Name == "Head" or Mouse.Target.Name == "Right Arm" or Mouse.Target.Name == "Left Arm" or Mouse.Target.Name == "Right Leg" or Mouse.Target.Name == "Left Leg" or Mouse.Target.Name == "Torso" or Mouse.Target.Name == "HumanoidRootPart" then 

Sorry if i explained it wrong or weirdly, i’m not that good at explaining things

2 Likes