Help with checking nearest seat to player

Hello, I need help, So this is scripted the functions is to :GetChildren() in a Folder to detect car, then a function detects the nearest seat, but the problem here, is that it’s only detecting like one Seat not all, it’s only placing the adornee to ONE Seat, not all!

GIF Showing error: https://gyazo.com/4318e760ef2b1282441713f8a7ad6a3c

Image of the Model: https://gyazo.com/90c51fd721e7db95fdb3a82ab7a87e2b

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")

local character = Players.LocalPlayer.Character

local ClosestVehicle, ClosestSeat, Gui, CurrentSeat

character.Humanoid.Died:Connect(function()
	if Gui then
		Gui:Destroy()
	end
	script.Disabled = true
end)

RunService.Heartbeat:Connect(function()
	for i,v in pairs(game.Workspace.Vehicles:GetChildren()) do
	local mag = (v.DriveSeat.Position - character.HumanoidRootPart.Position).Magnitude
	if mag <= 20 then
      ClosestVehicle = v
       if v then
	CheckForNearestSit(ClosestVehicle)
	print(ClosestSeat)
	end
end
	end
end)

UserInputService.InputBegan:Connect(function(Key, GameProcessed)
	if Key.KeyCode == Enum.KeyCode.E then
		if (ClosestSeat.Position - character.HumanoidRootPart.Position).Magnitude <= 6 then
			if CurrentSeat == nil then
				CurrentSeat = Gui.Adornee
			CurrentSeat:Sit(character.Humanoid)
			else
			
				CurrentSeat:Sit(nil)
				CurrentSeat = nil
			end
		end
	end
end)

function CheckForNearestSit(Vehicle)
	for i,v in pairs(Vehicle:GetChildren()) do
		if v:IsA("Seat") or v:IsA("VehicleSeat") then
			local mag = (v.Position - character.HumanoidRootPart.Position).Magnitude
			if mag <= 6 then
				ClosestSeat = v
				Gui = script.Enter
				Gui.Adornee = v or ClosestSeat
			else
				Gui = script.Enter
				Gui.Adornee = nil
				Gui = nil
			end
		end
	end
end
1 Like

You’ll need to store all distances in a local array, and check which one was closest at the end.

You can use a dictionary array for this.
Seats[v] = distance.

Wait, What do you mean, lol apologies

He is saying that you should store the distances of all of them, then compare them to find the nearest at the end.

What you are currently doing is you are setting any object that is closer than a certain number as the closest, even though there may be more than one object closer than that number, so you may not end with the truly closest.

Try this, your function doesn’t look like it respects any existing chair. Basically:

Code on the first seat: Oh I see this chair, it’s magnitude is less than 6. This is the closest seat!
Code on the last seat: The last seat has a magnitude of 7. forgets about the closest seat (by setting Gui to nil) There is no closest seat!

function CheckForNearestSit(Vehicle)
	for i,v in pairs(Vehicle:GetChildren()) do
		if v:IsA("Seat") or v:IsA("VehicleSeat") then
			local mag = (v.Position - character.HumanoidRootPart.Position).Magnitude
			if mag <= 6 then
				ClosestSeat = v
				Gui = script.Enter
				Gui.Adornee = v or ClosestSeat
				return; -- stop everything when you find the seat
			end
		end
	end -- set it to nothing if it finds no results
	Gui = script.Enter
	Gui.Adornee = nil
	Gui = nil
end

Let me know if anything happens.

Thank you so much. Later I’ll try it and tell if I found any trouble! Edit: Yes, it worked!! thank you so much.

1 Like