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

How do I make it so an action can only happen if I am in a specific radius?

Asked by 4 years ago

I am trying to make a fishing simulator and let the code comments speak for themselves:

01local debounce = false
02 
03local whoClicked = script.Parent.Parent.Parent
04 
05local starterRod = whoClicked.Backpack.Rod
06 
07starterRod.Activated:Connect(function()
08 
09    if debounce == false then
10 
11        --I need an if distance from the part is <30 then
12 
13        debounce = true
14 
15        local Rope = Instance.new("RopeConstraint", workspace)
View all 45 lines...

1 answer

Log in to vote
0
Answered by
Mineloxer 187
4 years ago

You can use the magnitude property like so:

01local part1 = -- some part
02local part2 = -- some other part
03local radius = 30 -- In studs
04 
05-- Get the positions
06local part1Pos = part1.Position
07local part2Pos = part2.Position
08 
09-- Calculate the distance between the parts using the magnitude property of Vector3's
10local distance = (part2Pos - part1Pos).Magnitude
11 
12-- Check if the distance is within the radius
13if distance <= radius then
14    -- Code goes here
15end

Tip: Use HumanoidRootPart inside of the character to represent the position as it's not affected by animations.

Here is the API reference for Vector3's, you can do more cool stuff with them:

https://developer.roblox.com/en-us/api-reference/datatype/Vector3

1
Tysm man! it really helped a ton Anderssc 22 — 4y
0
Np, glad I could help! Mineloxer 187 — 4y
Ad

Answer this question