I am trying to make a fishing simulator and let the code comments speak for themselves:
01 | local debounce = false |
02 |
03 | local whoClicked = script.Parent.Parent.Parent |
04 |
05 | local starterRod = whoClicked.Backpack.Rod |
06 |
07 | starterRod.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) |
You can use the magnitude property like so:
01 | local part 1 = -- some part |
02 | local part 2 = -- some other part |
03 | local radius = 30 -- In studs |
04 |
05 | -- Get the positions |
06 | local part 1 Pos = part 1. Position |
07 | local part 2 Pos = part 2. Position |
08 |
09 | -- Calculate the distance between the parts using the magnitude property of Vector3's |
10 | local distance = (part 2 Pos - part 1 Pos).Magnitude |
11 |
12 | -- Check if the distance is within the radius |
13 | if distance < = radius then |
14 | -- Code goes here |
15 | end |
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