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

BackStab script feedback?

Asked by 8 years ago

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)

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

First: tab your code correctly. This isn't super hard to do, and it makes code much easier to work with.

Next: use local variables. It's always a good idea to declare your intent by explicitly declaring variables using local. It also saves headaches later.

Make your code much easier to read by putting spaces around operators. At the least, spaces around = and + make it significantly easier to parse.

distance is a poor name for a displacement

normDistance is a poor name for something that's always length 1 -- call it a direction.

dotProduct is superfluous. Just use direction:Dot(humanoid.Torso.CFrame.lookVector)

Use the two argument Instance.new(class, parent) to save a line.

Use workspace instead of game.Workspace.

Use :FindFirstChild instead of :findFirstChild.


EDIT:

  • hit.Parent.Humanoid will never be nil. You will get an error if the object doesn't exist. Use :FindFirstChild("Humanoid") instead.

  • It's pointless to use :FindFirstChild("Torso").Position. The value of not erroring when the torso doesn't exist goes away if you try to ask for the .Position of nil. Just use .Torso.Position, or do proper error checking like it sounds like you need.

0
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? threatboy101 2 — 8y
0
nvm my comment so you meant vTorso is acting as a Origin for Torso hence a displacement threatboy101 2 — 8y
0
I keep getting an error on this line: local TorsoPos=hit.Parent:findFirstChild("Torso").Position keeps saying that torso is a nil value threatboy101 2 — 8y
0
to be more specific : Players.Player1.Backpack.LinkedSword.SwordScript:45: attempt to index a nil value threatboy101 2 — 8y
Ad

Answer this question