Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index nil with torso?

Asked by 4 years ago

I'm trying to make a script that welds a part to the player, the part that I'm trying to weld has scripts and other things inside of it. In output I am getting the error below, and I do not know why it is happening. Any help?

Error: attempt to index nil with 'Torso'

Script:

game.Players.PlayerAdded:connect(function(plr)  

local p = game.ReplicatedStorage:FindFirstChild("Character2"):clone()
p.Parent = plr.Character
p.CFrame = CFrame.new(plr.Character.Torso.Position)
p.Anchored = false

local Y = Instance.new("Weld")
Y.Part0 = plr.Character.Torso
Y.Part1 = p
Y.Parent = Y.Part0
    plr.CharacterAdded:connect(function(char)   
    end)
end)
0
Do the player have R6 rig? BestCreativeBoy 1395 — 4y

3 answers

Log in to vote
1
Answered by 4 years ago

the error attempt to index nil with 'Torso' basically just means that nothing is being found, nothing is there and plr.Character is nil. I suggest looking back at that, and seeing why it's not functioning correct. Also, :connect(), and :clone() are good but I suggest changing them to :Connect() and :Clone(), because they're technically deprecated so. If you need me just respond here or add me on Discord, I can help you more. I personally think that the Torso of the charcter is not there, and or you're using p.CFrame = CFrame.new() wrong. Also, for local p do just

local p = game.ReplicatedStorage.Character2
p:Clone()
p.Parent = plr.Character

if plr.Character.Torso ~= nil then
    print("Not Equal to Nil")
    p.CFrame = CFrame.new(plr.Character.Torso.Position)
p.Anchored = false
else
    print("It's nil")
end

this code above checks if the players torso is equal to nil if it is then it will print("It's nil") and you know your answer, if it's not and it prints Not Equal To Nil then add a wait for child on character. If that isn't working check your properties and the stuff you're setting.

Hope this helped, if not let me know!

Ad
Log in to vote
1
Answered by
sleazel 1287 Moderation Voter
4 years ago

Replace line 04 with this brilliant statement:

p.Parent = plr.Character or plr.CharacterAdded:Wait()

Also make sure your game is set to use R6, as R15 does not have a Torso. (it has UpperTorso and LowerTorso)

Log in to vote
0
Answered by 4 years ago

In most cases, the character does not load in when the player joins, so you should wait for the character using repeat wait() until player.Character or some other method that you can find

0
Brandon1881 is true, but if the torso is nil then it will not function. You can add a character added event in. Nicholas1088 169 — 4y

Answer this question