Answered by
6 years ago Edited 6 years ago
Other ways
you can use magnitude to detect if a player is in range to punch. though theres a function called player:DistanceFromCharacter(vector3 value)
, this will make the magnitude for you so then you can determine if the target is in a certain range or not.
Better methods and deprecated stuff
:connect()
is deprecated use :Connect()
instead. also you should use local variables instead of global ones as local variables are faster and you'd rarely need to use global variables anyways. :findFirstChild()
is deprecated so use :FindFirstChild()
; deprecation. use the :GetPropertyChangedSignal()
function over while loops to reduce lag; :GetPropertyChangedSignal()
is pretty much the same thing as Changed
, though it listens if a certain property changes.
an example:
03 | local PunchHit = script.Parent.Parent.PunchHit |
04 | local players = game.Players:GetPlayers() |
05 | local player = game.Players.LocalPlayer |
06 | local part = workspace.Part |
08 | local function onChange() |
09 | if PunchHit.Value = = true then |
10 | for _,plr in pairs (players) do |
12 | local distance = plr:DistanceFromCharacter(part.CFrame.Position) |
14 | if distance < = 4.5 then |
15 | plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - 50 |
23 | PunchHit:GetPropertyChangedSignal( "Value" ):Connect(onChange) |
:DistanceFromCharacter()
gets the distance between the player's head and the vector3 value if ur wondering. LocalScripts only run in certain places such as inside the player's character, StarterGui, StarterPlayer, ReplicatedFirst and StarterPack.
to do this on the server, use the player parameter to get the player instead of LocalPlayer