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)
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!
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)
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