I feel stupid for asking this but rn its like 3 in the morning and I just want to be over with this.
Basically im making something which checks a table to see which number in the table is the closest to the mouses z position. I have it working with negative values but positives are a no go and im not sure how to fix this.
Code:
local conenct4Plane = Instance.new("Part")
local mouseHoverTable = {}
for i,x in gridTable do
local z = x.Position.Z
table.insert(mouseHoverTable,z)
end
local closestZ = math.huge
for _,position in mouseHoverTable do
if math.abs(mouse.Hit.Position.Z-2) < math.abs(closestZ) then
closestZ = position
end
end
mousePart.Position = Vector3.new(connect4Plane.Position.X-4,connect4Plane.Position.Y,closestZ)
local connect4Plane = Instance.new("Part")
local mouseHoverTable = {}
for i, x in gridTable do
local z = x.Position.Z
table.insert(mouseHoverTable, z)
end
function findClosestNumber(number, table)
local closestDifference = math.huge
local closestNumber = nil
for _, value in pairs(table) do
local difference = math.abs(number - value)
if difference <= closestDifference then
closestDifference = difference
closestNumber = value
end
end
return closestNumber
end
local closestZ = findClosestNumber(mouse.Hit.Position.Z, mouseHoverTable)
mousePart.Position = Vector3.new(connect4Plane.Position.X - 4, connect4Plane.Position.Y, closestZ)
Loop comparison may be a bit off vs comparing the absolute differences between…
local connect4Plane=Instance.new("Part")
local mouseHoverTable={}
for i,x in gridTable do
local z=x.Position.Z
table.insert(mouseHoverTable,z)
end
local closestZ=math.huge
for _,position in mouseHoverTable do
if math.abs(mouse.Hit.Position.Z-position)<math.abs(mouse.Hit.Position.Z-closestZ)then
closestZ=position
end
end
mousePart.Position=Vector3.new(connect4Plane.Position.X-4,connect4Plane.Position.Y,closestZ)