Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

BackStab script issues?

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

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.

2 comments

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.