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

is it possible to detect how far a player is from a part?

Asked by
Smach28 10
7 years ago

So Idk if its possible but just wanted to ask, I am making this gun that if a player is 5 studs away from it, when activated they die, is that possible?

0
Just make the ray shorter. Im assuming you are using Raycasting. hiimgoodpack 2009 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

Yes, it is possible. You need to use something called magnitudeto detect how far away the player is from a part.

local camera = game.Workspace.CurrentCamera
local part = workspace.SomePart --Locate your part here
local studsAway = 18 --set how many studs you the player needs to be in range of
game:GetService("RunService").RenderStepped:connect(function()
    if (camera.CoordinateFrame.p - part.Position).magnitude < studsAway then 
--do something
print("hey")
    end
end)

**Please accept my answer if this helped! **

0
I have one more question, will it also detect the player holding the gun? Smach28 10 — 7y
0
And what's the code I put in it to kill the person. Smach28 10 — 7y
0
You'll have to learn and do that yourself, so I suggest reading the wiki. This was an example on how to use magnitude so you could add onto it. PyccknnXakep 1225 — 7y
0
ok Smach28 10 — 7y
Ad
Log in to vote
0
Answered by 7 years ago
local magnitude = (part1.Position - part2.Position).magnitude
print(magnitude)
if math.floor(magnitude) < 5 then
    --code
end

.magnitude calculates the distance between two parts, hope this helped you! accept if it helped you solve your problem!

0
It's best to get the player's camera and use RenderStepped though. PyccknnXakep 1225 — 7y
0
Also CoordinateFrame. PyccknnXakep 1225 — 7y

Answer this question