Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there a way to make a Press E to sit localscript when near a object?

Asked by 5 years ago

So I have a BillboardGUI that when I'm getting near it (distance of 10) A ImageLabel with press E pops up. I cant seem to find or create a localscript so that when the player stays near it, the character will Sit on that seat by pressing E. I;'ve tried many codes and even tried creating my own one but I;m bad at lua.. Details: LocalScript (I guess), I think I need a magnitude or sometihng to measure the distance between the character and the seat... I'm really confused and I'm bad at lua. Pls help!

0
yea magnitude for distance. what bits are you confused on? royaltoe 5144 — 5y

1 answer

Log in to vote
4
Answered by
Alphexus 498 Moderation Voter
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.

01-- // Services \\ --
02local UserInputService = game:GetService("UserInputService")
03local Players = game:GetService("Players")
04 
05-- // Variables \\ --
06local Player = Players.LocalPlayer
07local Character = Player.Character or Player.CharacterAdded:Wait() -- // If character isn't loaded wait till its added
08 
09local Seats = workspace.Seats -- // Seats folder where all the seats
10local RANGE = 8 -- // How close you have to be to a seat in order to press E.

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.

01-- // Services \\ --
02local UserInputService = game:GetService("UserInputService")
03local Players = game:GetService("Players")
04 
05-- // Variables \\ --
06local Player = Players.LocalPlayer
07local Character = Player.Character or Player.CharacterAdded:Wait() -- // If character isn't loaded wait till its added
08local Humanoid = Character:WaitForChild("Humanoid")
09 
10local Seats = workspace.Seats -- // Seats folder where all the seats
11local RANGE = 8 -- // How close you have to be to a seat in order to press E.
12 
13-- // Functions \\ --
14local function GetClosestSeat()
15    local SeatsInRange = {}
View all 31 lines...

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.

01-- // Services \\ --
02local UserInputService = game:GetService("UserInputService")
03local Players = game:GetService("Players")
04 
05-- // Variables \\ --
06local Player = Players.LocalPlayer
07local Character = Player.Character or Player.CharacterAdded:Wait() -- // If character isn't loaded wait till its added
08local Humanoid = Character:WaitForChild("Humanoid")
09 
10local Seats = workspace.Seats -- // Seats folder where all the seats
11local RANGE = 8 -- // How close you have to be to a seat in order to press E.
12 
13-- // Functions \\ --
14local function GetClosestSeat()
15    local SeatsInRange = {}
View all 42 lines...

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!

0
Impressive Ziffixture 6913 — 5y
0
Players.qMrSpooky.PlayerGui.LocalScript:25: Incomplete statement: expected assignment or a function call - This error pops up and sends me to line 25 of the code... But I still want to thank you! How can I fix the error? I made a folder "Seats" and put the seats in it... I also put the localscript in StarterGUI. Do i need a DriverSeat or normal Seat? qMrSpooky 20 — 5y
0
I'm too annoying. Sorry. qMrSpooky 20 — 5y
0
Sorry but just add “return” before a.Mag and it should fix it Alphexus 498 — 5y
0
tysm qMrSpooky 20 — 5y
Ad

Answer this question