Standard magnitude checking systems are boring and inefficient, often checking through parts that don’t even need checking, therefor I tried to create the most efficient ‘closest point finder’ system I possibly good, while not compromising on quality.
- What does the code do and what are you not satisfied with?
It finds the closest part on the map to the local player - What potential improvements have you considered?
Ive tried everything I could to optimise it as much as possible - How (specifically) do you want to improve the code?
Anything that could make it faster / more efficient.
--[[ Variables ]]--
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local MapSize = 600
local BoxSize = 50
local SlowCheck = 1.5
local FastCheck = .1
local HalfSize = MapSize/2
local HalfBox = BoxSize/2
local CheckRate = SlowCheck / FastCheck
local Origin = Vector2.new(0,0)
local Points = {}
local NearPoints = {}
--[[ Actual code ]]--
local function MakePart(Point, Color)
local Part = Instance.new("Part")
Part.Anchored = true
Part.Size = Vector3.new(1,1,1)
Part.Position = Point
Part.Parent = game.Workspace
Part.Color = Color
return Part
end
local function RoundTo10(Position)
return Vector2.new((math.floor(Position.X/HalfBox+.5)*HalfBox), (math.floor(Position.Z/HalfBox+.5)*HalfBox))
end
local function DrawGrids()
local X = MapSize/HalfBox
local Y = X
local StartPoint = Origin - Vector2.new(HalfSize,HalfSize)
for Xcount = 0, X do
local Xpos = Xcount * BoxSize
for Ycount = 0, Y do
local Ypos = Ycount * BoxSize
local xpoint = StartPoint.X + Xpos/2
local ypoint = StartPoint.Y + Ypos/2
Points[(xpoint).. (ypoint)] = {}
end
end
end
local function AddObject(Object)
local MainPoint = Object.PrimaryPart
local NewPos = RoundTo10(MainPoint.Position)
local Point = NewPos.X.. NewPos.Y
local Table = Points[Point]
if Table then
table.insert(Points[Point], Object)
end
end
local function ArangeExsistingInteractables()
for i,v in pairs(game.Workspace.Interactables:GetChildren()) do
AddObject(v)
end
end
local function FindNearest()
local MainPoint = Player.Character.PrimaryPart.Position
local ClosestLength = 1000
local ClosestObject = nil
for _, v in pairs(NearPoints) do
local Mag = (MainPoint - v.PrimaryPart.Position).magnitude
if Mag < ClosestLength then
ClosestLength = Mag
ClosestObject = v
end
end
return ClosestObject
end
DrawGrids()
ArangeExsistingInteractables()
game.Players.LocalPlayer.CharacterAdded:Wait()
while RunService.Heartbeat:Wait() do
local NewPos = RoundTo10(Player.Character.PrimaryPart.Position)
NearPoints = {}
local PointTable = {
NewPos.X.. NewPos.Y,
NewPos.X.. NewPos.Y + HalfBox,
NewPos.X.. NewPos.Y - HalfBox,
NewPos.X + HalfBox.. NewPos.Y,
NewPos.X - HalfBox.. NewPos.Y,
NewPos.X - HalfBox.. NewPos.Y - HalfBox,
NewPos.X + HalfBox.. NewPos.Y + HalfBox,
NewPos.X - HalfBox.. NewPos.Y + HalfBox,
NewPos.X + HalfBox.. NewPos.Y - HalfBox,
}
for _, Point in pairs(PointTable) do
for _,v in pairs(Points[Point]) do
table.insert(NearPoints, v)
end
end
for _ = 1, CheckRate do
local Closest = FindNearest()
for i,v in pairs(game.Workspace.Interactables:GetChildren()) do
v.PrimaryPart.Color = Color3.fromRGB(163, 162, 165)
end
if Closest then
Closest.PrimaryPart.Color = Color3.new(0,0,1)
end
wait(FastCheck)
end
end