You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
So I want to get a script where when the E Key is pressed the Closest part to the player will be printed in the output,
- What is the issue? Include screenshots / videos if possible!
when i test the script the output comes up with two parts instead of one
in these images the part closest part is Part1 but the script still choses to print the other part even if part1 is closer to the player than part
However when i position the player on the other side of the two parts it ends up working the way i want it to
- What solutions have you tried so far?
So i have tried looking at other scripts on finding the closest part but I couldnt seem to find out how to get it to work
-- This is The Script i used
local userInputService = game:GetService("UserInputService")
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.E then
print("E was pressed")
local player = game.Players.LocalPlayer
local NearestPart = nil
local Lenght = nil
local Folder = workspace.Folder
local Player = game.Players.LocalPlayer
wait(0.5)
for i,v in pairs(Folder:GetChildren()) do
local LenghtX = Player:DistanceFromCharacter(v.Position)
if Lenght == nil or LenghtX < Lenght then
Lenght = LenghtX
NearestPart = v
print(NearestPart.name, player:DistanceFromCharacter(NearestPart.Position))
end
end
end
end
end)
Any Help would be appreciated