Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Output error attempt to index local 'torso1'..?

So I was trying to make this system where it checks for your character if it is R6 or R15, but it returns this > "Attempt to index local 'torso1' (a nil value)" .> Does that mean the so called "torso" equals nil?

Here is my code:

local part = script.Parent
local box = script.Parent.Parent.Box1
local board = script.Parent.Parent.Board
local settings = script.Parent.Parent.Settings
function Main1(otherPart)
local char = otherPart.Parent
if char then
local torso1 = char:FindFirstChild("Torso")
torso1.Anchored = true
else
local torso1 = char:FindFirstChild("LowerTorso")
torso1.Anchored = true	
		
		
end
	
char.Name =  settings.PlayerNames
	
for i,v in pairs (box:GetChildren()) do
v.CanCollide = false
end
	
end

script.Parent.Touched:connect(Main1)

Thanks for your answers!

2 comments

0

fix indentation pls

0

fixed

3 answers

✓ Accepted answer

SOLUTION 1: CHECKING PARTS

You're trying to check if the character is R6 or R15, so you should be checking if the character has a part called "LowerTorso" or not. However, your script appears to instead check if the character exists:

local char = otherPart.Parent
if char then

Note: this will always be true, because you're checking if otherPart's parent isn't nil, in a function that is triggered by otherPart touching part - and a part with a nil parent can't touch anything, so the event would not be triggered.

Here's how you can check which torso exists inside the character:

local torso1 = char:FindFirstChild("Torso")
local torso2 = char:FindFirstChild("LowerTorso")

if torso1 then
	-- it's R6
elseif torso2 then
	-- it's R15
end

A few suggestions:

  • when referring to a child, use :WaitForChild, so you can avoid errors if the child hasn't loaded yet.

  • :connect is deprecated - use :Connect.

  • I noticed you didn't use the i variable in your for loop. In such cases, it is recommended to use _ - basically the trash can variable.

  • you can declare the function inside the :Connect

Here's the fixed code:

local part = script.Parent
local box = part.Parent:WaitForChild("Box1")
local board = part.Parent:WaitForChild("Board")
local settings = part.Parent:WaitForChild("Settings")

part.Touched:Connect(function(otherPart)
	local char = otherPart.Parent
	local torso1 = char:FindFirstChild("Torso")
	local torso2 = char:FindFirstChild("LowerTorso")

	if torso1 then
		torso1.Anchored = true
	elseif torso2 then
		torso2.Anchored = true
	end

	char.Name =  settings.PlayerNames

	for _,v in pairs(box:GetChildren()) do
		v.CanCollide = false
	end
end)

SOLUTION 2: RIGTYPE

Very similiar. Here you check if the humanoid exists, and then its RigType.

local part = script.Parent
local box = part.Parent:WaitForChild("Box1")
local board = part.Parent:WaitForChild("Board")
local settings = part.Parent:WaitForChild("Settings")

part.Touched:Connect(function(otherPart)
	local char = otherPart.Parent
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	
	if humanoid then
		if humanoid.RigType == Enum.HumanoidRigType.R6 then
			humanoid:WaitForChild("Torso").Anchored = true
		elseif humanoid.RigType == Enum.HumanoidRigType.R15 then
			humanoid:WaitForChild("LowerTorso").Anchored = true
		end
	end

	char.Name =  settings.PlayerNames

	for _,v in pairs(box:GetChildren()) do
		v.CanCollide = false
	end
end)

2 comments

0

Worked flawlessly. Thank you!

0

edit #1: added second solution

Here's the problem:

As you use the line

local torso1 = char:FindFirstChild("Torso")

then the script will execute if it FOUND THE OBJECT. If not, the torso will be nil

But as you add another line like this:

local torso1 = char:FindFirstChild("Torso")
torso1.Anchored = true

then the script executes the second line, which the torso1 was nil so it will cause an error. Get it?

Here's an unstable fix:

local torso1 = char:WaitForChild("Torso") --Use WaitForChild
torso1.Anchored = true

2 comments

0

Good point. But as I try this solution, it returns an "infinite yield", does that mean I have to use wait()?

0

It means the object you were searching for was not found within the 5 second wait period.

game:GetService("Players").PlayerAdded:Connect(function(player)
	wait(1)
	print(player.Character.Humanoid.RigType)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.