How can I know which of the specific models I have near to the player is near to the player?

  1. What do you want to achieve? I have some specific models near the player, I want to know which one is near to the player.

  2. What is the issue? I don’t know how to know which model is near to the player

  3. What solutions have you tried so far? I tried checking the magnitude unit for every model with a for loop, but It didn’t work.

Here is a random draw I made for explaining

You can do something like this:

local player = -- however you get player
local maxDistance = 15 -- only get models 15 or less studs away
local closest

for _, model in pairs(folderOfModels:GetChildren()) -- its best to have a folder of the models you want to check as to not loop through the entire workspace
     local dist = (player.Character.HumanoidRootPart.Position - model.PrimaryPart.Position).Magnitude -- model must have primarypart
     if dist < maxDistance then
          maxDistance = dist
          closest = model
     end
end
if closest then
     print("The closest model is .. " closest.Name)
else
     print("No models in range")
end
2 Likes

I tried your script, and it works! Thank you very much.

1 Like