I find people often making team fortress scripts and asking for backstab scripts i know i could have used math.acos and :Dot() function but i did not want to deal with radians. But i would appreciate some feedback on what i could have done better. I did not post this on the roblox forum because they always bash.
tool=script.Parent
sword=tool.Handle
sword.Touched:connect(function(hit)
if hit.Parent ~= tool.Parent and hit.Parent.Humanoid ~= nil then
humanoid=hit.Parent:findFirstChild("Humanoid")
vCharacter=tool.Parent
TorsoPos=hit.Parent:findFirstChild("Torso").Position --the torso of the guy you're about kill
vTorsoPos=vCharacter:findFirstChild("Torso").Position --your torso
Player=Game.Players:GetPlayerFromCharacter(hit.Parent)
vPlayer=Game.Players:GetPlayerFromCharacter(vCharacter)
if Player.TeamColor ~= vPlayer.TeamColor then
humanoid.Died:connect(function()
local distance=vTorsoPos-TorsoPos
local direction=distance/distance.magnitude
local dp=dotProduct(normDistance,humanoid.Torso.CFrame.lookVector)
print(dp)
if dp < -0.1 then
--increase the value to increase the range
-- but don't make it 0 or greater
local m=Instance.new("Message")
m.Parent=game.Workspace
m.Text="BACKSTAB!!!"
wait(2)
m:Destroy()
end
end)
end
end
end)

4 comments
The formatting of sh is part of the reason my code is out of place and what you mean distance is a poor name for displacement?
nvm my comment so you meant vTorso is acting as a Origin for Torso hence a displacement
I keep getting an error on this line: local TorsoPos=hit.Parent:findFirstChild("Torso").Position keeps saying that torso is a nil value
to be more specific : Players.Player1.Backpack.LinkedSword.SwordScript:45: attempt to index a nil value