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)

2 comments
yes, i did that the error does not show up anymore but for some reason its not showing the message in some clear backstab cases
You may have put this in the wrong place. Put local TorsoPos at the beginning of the script. and the if then statement right before line 45