Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

1 comment

0

Just make the ray shorter. Im assuming you are using Raycasting.

2 answers

✓ Accepted answer

Yes, it is possible. You need to use something called magnitude to 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! **

4 comments

0

I have one more question, will it also detect the player holding the gun?

0

And what's the code I put in it to kill the person.

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.

0

ok

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!

2 comments

0

It's best to get the player's camera and use RenderStepped though.

0

Also CoordinateFrame.

This discussion is preserved as it was. New answers, comments, and votes are closed.