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

How to detect an object within a radius?

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?

1 comment

0

magnitude

1 answer

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!

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