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

BackStab script issues?

Asked by 9 years ago

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)

1 answer

Log in to vote
0
Answered by 9 years ago
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.

0
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 threatboy101 2 — 9y
0
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 HungryJaffer 1246 — 9y
Ad

Answer this question