Answered by
5 years ago Edited 5 years ago
Yes, you will need to know 3 things to create this. Magnitude which is basically the length of a Vector. The method Sit(humanoid) for a Seat Instance. You will also need to know how InputBegan event works from UserInputService.
First, put all your Seats in a folder that's in workspace. Insert a local script into StarterGui(or wherever local scripts run). Let's start by defining a few variables.
02 | local UserInputService = game:GetService( "UserInputService" ) |
03 | local Players = game:GetService( "Players" ) |
06 | local Player = Players.LocalPlayer |
07 | local Character = Player.Character or Player.CharacterAdded:Wait() |
09 | local Seats = workspace.Seats |
Next, we are going to need to make one function which is going to be called GetClosestSeat().
We are going to loop through all of the Seats and check the magnitude of the (Seat.Position-Character.HumanoidRootPart.Position). If the magnitude is less than or equal to the range, we will insert the Seat along with the magnitude in a table. Why a table? Well, what if two seats are in range. We want to sit in the closest seat, so we use a table to collect all the seats in range and then sort them according to how close they are.
02 | local UserInputService = game:GetService( "UserInputService" ) |
03 | local Players = game:GetService( "Players" ) |
06 | local Player = Players.LocalPlayer |
07 | local Character = Player.Character or Player.CharacterAdded:Wait() |
08 | local Humanoid = Character:WaitForChild( "Humanoid" ) |
10 | local Seats = workspace.Seats |
14 | local function GetClosestSeat() |
15 | local SeatsInRange = { } |
16 | for _,seat in pairs (Seats:GetChildren()) do |
17 | local mag = (seat.Position-Character.HumanoidRootPart.Position).Magnitude |
19 | table.insert(SeatsInRange, { Seat = seat, Mag = mag } ) |
23 | if #SeatsInRange > 0 then |
24 | table.sort(SeatsInRange, function (a,b) |
27 | return SeatsInRange [ 1 ] .Seat |
Now, we are going to need to use InputBegan from UserInputService and call the function to see if there is a seat in range and sit on it.
02 | local UserInputService = game:GetService( "UserInputService" ) |
03 | local Players = game:GetService( "Players" ) |
06 | local Player = Players.LocalPlayer |
07 | local Character = Player.Character or Player.CharacterAdded:Wait() |
08 | local Humanoid = Character:WaitForChild( "Humanoid" ) |
10 | local Seats = workspace.Seats |
14 | local function GetClosestSeat() |
15 | local SeatsInRange = { } |
16 | for _,seat in pairs (Seats:GetChildren()) do |
17 | local mag = (seat.Position-Character.HumanoidRootPart.Position).Magnitude |
19 | table.insert(SeatsInRange, { Seat = seat, Mag = mag } ) |
23 | if #SeatsInRange > 0 then |
24 | table.sort(SeatsInRange, function (a,b) |
27 | return SeatsInRange [ 1 ] .Seat |
33 | UserInputService.InputBegan:Connect( function (Input, GameProcessed) |
34 | if GameProcessed then return end |
36 | if Input.KeyCode = = Enum.KeyCode.E and Humanoid.Sit = = false then |
37 | local ClosestSeat = GetClosestSeat() |
38 | if ClosestSeat ~ = nil then |
39 | ClosestSeat:Sit(Humanoid) |
I hope this helped you. I am in school right now and don't have time to test this, but looking at it, it should work. If you have any problems, feel free to ask. I would appreciate if you accept this answer if it worked out for you. Thanks!