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

BackStab script feedback?

Asked by 9 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.

01tool=script.Parent
02sword=tool.Handle
03 
04sword.Touched:connect(function(hit)
05 if hit.Parent ~= tool.Parent and hit.Parent.Humanoid ~= nil then
06 
07   humanoid=hit.Parent:findFirstChild("Humanoid")
08   vCharacter=tool.Parent
09   TorsoPos=hit.Parent:findFirstChild("Torso").Position   --the torso of the guy you're about kill
10   vTorsoPos=vCharacter:findFirstChild("Torso").Position  --your torso
11   Player=Game.Players:GetPlayerFromCharacter(hit.Parent)
12   vPlayer=Game.Players:GetPlayerFromCharacter(vCharacter)
13 
14   if Player.TeamColor ~= vPlayer.TeamColor then
15         humanoid.Died:connect(function()
View all 37 lines...

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 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 — 9y
0
nvm my comment so you meant vTorso is acting as a Origin for Torso hence a displacement threatboy101 2 — 9y
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 — 9y
0
to be more specific : Players.Player1.Backpack.LinkedSword.SwordScript:45: attempt to index a nil value threatboy101 2 — 9y
Ad

Answer this question