Sometimes the script works other times i get the error Players .Player1 .Backpack .LinkedSword .SwordScript:45: attempt to index a nil value that is this line local TorsoPos hit .Parent:findFirstChild("Torso").Position please help it's been bothering me for quite a while.
tool=script.Parent sword=tool.Handle sword.Touched:connect(function(hit) if hit.Parent ~= tool.Parent then local Character = hit.Parent local humanoid = Character:findFirstChild("Humanoid") local vCharacter = tool.Parent if Character:findFirstChild("Torso") then local TorsoPos = Character:findFirstChild("Torso").Position end local vTorsoPos = vCharacter:findFirstChild("Torso").Position local Player = Game.Players:GetPlayerFromCharacter(Character) local vPlayer = Game.Players:GetPlayerFromCharacter(vCharacter) if Player.TeamColor ~= vPlayer.TeamColor then humanoid.Died:connect(function() local distance = vTorsoPos-TorsoPos local normDistance = distance/distance.magnitude local dp dp = normDistance:Dot(humanoid.Torso.CFrame.lookVector) print(dp) if dp < -0.1 then local m = Instance.new("Message",Workspace) local name = Character.Name m.Text = vCharacter.Name.."backstabbed"..name wait(2) m:Destroy() end end) end end end)
local TorsoPos if hit.Parent:findFirstChild("Torso") then local TorsoPos = hit .Parent:findFirstChild("Torso").Position end
I added a check if there is a Torso, if there is then it'll name the TorsoPos variable.