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

How to detect an object within a radius?

Asked by 6 years ago

so what i want to do is to teleport a player to another place BUT only if they are within the radius of an object, i know i have to use DistanceFromCharacter, but i dont know how to do if the character is within the radius of an object.

simplified, i want to do this: If A (character) is within a 6-stud radio of B (object) and A presses a key, A is teleported to C (location)

any ideas?

0
magnitude DeceptiveCaster 3761 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

So the idea is pretty simple, using magnitude, you can check the distance between any two vectors (or two parts) Using a loop, you would repeatedly check if they are in range.

local p = game:GetService("Players").LocalPlayer
p.CharacterAdded:Wait()
local c = p.Character
local t = c:FindFirstChild("Torso") or c:FindFirstChild("UpperTorso")
local part = workspace.Part --This is the part you are checking the distance for

--any loops would be from here on

local magnitude = (t.Position - part.Position).magnitude --the order of the subtraction does not matter

if magnitude < 6 then --(the distance in studs) how far the two parts are from each other and are withing 6 studs of each other 
    --your code
end

Hope this helps you!

Ad

Answer this question