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

How to detect distance?

Asked by
emite1000 335 Moderation Voter
10 years ago

How do you make it so that a brick or model can tell when a part is near them (say, 50 studs away). I'm really stumped by this problem, although I suspect it has something to do with using the Magnitude between bricks.

I'm creating a script so that when a part is x far away, a stuff happens.

2 answers

Log in to vote
2
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

Yes, you would use Magnitude to get the distance between two parts. I provided a script to help you out.

For a player: This would be a local script in Workspace. This will work for each individual player.

torso = game.Players.LocalPlayer.Character.Torso
part2 = game.Workspace.Part2

if (torso.Position - part2.Position).magnitude >= 40) then
    print('It is 40 studs away!!!')
end
0
But if I don't know what Part1 is (such as any player on the server), how do I make it print that when any player gets >= 40 studs away. emite1000 335 — 10y
0
This would be a local script and the part1 path (Could change the variable) could lead to the player's torso (game.Players.LocalPlayer.Character.Torso). Shawnyg 4330 — 10y
0
Yes, but what if I don't know the Player is what I'm saying. I want it to be any of the 20 players on the server so I can't just make a path to a defined part. emite1000 335 — 10y
0
For the Player's torso, you can. I'll update the script with an example. Shawnyg 4330 — 10y
View all comments (6 more)
0
Ohhhhh. Using LocalPlayer! I get it now! thanks! emite1000 335 — 10y
0
Actually, it turns out it doesn't work. emite1000 335 — 10y
0
Do you get any errors? I used this same way earlier with another script and it worked like a charm. Shawnyg 4330 — 10y
0
Try putting it in StarterGui. Shawnyg 4330 — 10y
0
Putting it in the StarterGui causes Studio to crash... emite1000 335 — 10y
0
Did you Publish it and test it by pressing 'Play'? Shawnyg 4330 — 10y
Ad
Log in to vote
0
Answered by
Vividex 162
10 years ago

For detecting the distance between an object and another object, preferably, I would use magnitude. This is a very good example of magnitude provided by ScriptOn

Answer this question