I'm making a turret that fires at people when they get too close. Is there an easy way to find the nearest player to the turret?
what i would do is make a table of all the players within the zone, and constantly check the positions of the player and the player that is the closest will be the target, this is most likely a inefficient way to do it, im open to suggestions from other people on better ways, if any, to get closest player
I did do this with a creepy eye project of mine before, just drop me a PM if you want the model
Here's a sample of how to find the nearest character:
local point = workspace.Turret --for example local closest local last = 0 for _,v in pairs(game:GetService("Players"):GetPlayers()) do local magnitude = (point.Position - v.Character.HumanoidRootPart.Position).magnitude if magnitude < last then closest = v end last = magnitude end