Ad
Log in to vote
1

How to detect the player's proximity to a specific block?

Asked by 7 years ago

I've been trying to figure out how to detect a player's proximity to a block, for purposes of radiation in a Fallout-esque game I've been making. I cannot think of how I would get the script to detect if the player is within the, say, 10-stud area around the block so the player gathers Radiation (of which I have being kept track of with an IntValue inside the character).

2 answers

Log in to vote
2
Answered by
Merely 2117 Moderation Voter Community Moderator
7 years ago

Use Player:DistanceFromCharacter. It accepts a Vector3 position.

For example, if you want to get the distance between the player and a block:

local block = game.Workspace.RadiationPart

local distance = player:DistanceFromCharacter(block.Position)

Note that it returns 0 if the player's character does not exist.

1
I almost feel stupid for not thinking of this. Thanks! iceman8k 15 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Another way to do this is using .magnitude (which can be used for any object instead of just the player)

local block = game.Workspace.RadiationPart

local distance = (player.Character.Torso.Position - block.Position).magnitude
0
Character is a model, so it doesn't have a Position property. You'll have to use the torso. Merely 2117 — 7y
0
I keep forgetting that x.x PiggyJingles 358 — 7y
1
Or player.Character:GetModelCFrame().p MrNicNac 855 — 7y

Answer this question