How to detect nearest anchor point for web swinging

Hello, DevForums! As you can tell from the past few posts, I have been trying to make a web swinging system that automatically finds anchor points to swing off of, but with little to no success. With this, I tried some ideas but also to no avail. How would I go about making a swinging system that automatically finds the nearest building to stick to?

1 Like

I hope you don’t mind me asking but have you got a process for finding the nearest building?
For example doing some raycasts to see what you hit and try and identify what contains the hit points?

I have tried using a for i,v in pairs to find all the parts, which I then detect which one is the closest to the player. The script looks something like:

For I, v in pairs(workspace.GetDescendants()) do
Distance = (v.Position - char.HumanoidRootPart.Position).magnitude
If distance <= 35 and distance >= 10 then
–Create raycast to v that gets the raycastresult.position and places a part there
End
End

(Sorry, I am not on my pc right now and I am on mobile)

The raycast works, however, not only is the part not placed at the nearest spot to the player and instead placed at the opposite side of the wall, but I feel that this is an extremely unreliable way to get an anchor point as the player will have to make it out of a certain range in order to place another anchor point, and if they are not far enough, then the web will connect to the old anchor point that was far behind them.

For more explanation, this script essentially searches for the nearest part in the workspace, and if it is within the range of the player, it creates a ray to the part it detected as valid to get the raycastResult.position, and sets anchor part position to be equal to this. What I wanted to happen was that it places the anchor part on the side of the wall, and it does, only for that it is on the opposite side of the wall.