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 4 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 — 4y

1 answer

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

-- // Services \\ --
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

-- // Variables \\ --
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- // If character isn't loaded wait till its added

local Seats = workspace.Seats -- // Seats folder where all the seats 
local 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.

-- // Services \\ --
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

-- // Variables \\ --
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- // If character isn't loaded wait till its added
local Humanoid = Character:WaitForChild("Humanoid")

local Seats = workspace.Seats -- // Seats folder where all the seats 
local RANGE = 8 -- // How close you have to be to a seat in order to press E.

-- // Functions \\ --
local function GetClosestSeat()
    local SeatsInRange = {}
    for _,seat in pairs(Seats:GetChildren()) do
        local mag = (seat.Position-Character.HumanoidRootPart.Position).Magnitude
        if mag <= RANGE then
            table.insert(SeatsInRange, {Seat = seat, Mag = mag}) -- // Insert into the table because the seat is in range.
        end
    end

    if #SeatsInRange > 0 then -- // If there are seats near the character.
        table.sort(SeatsInRange, function(a,b) -- // Sort the table, so the closest seat is the first index of the table.
            a.Mag < b.Mag 
        end)
        return SeatsInRange[1].Seat -- // Return the closest seat after sorting.
    else
        return nil -- // Return nil if no seats are close.
    end
end

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.

-- // Services \\ --
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

-- // Variables \\ --
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait() -- // If character isn't loaded wait till its added
local Humanoid = Character:WaitForChild("Humanoid")

local Seats = workspace.Seats -- // Seats folder where all the seats 
local RANGE = 8 -- // How close you have to be to a seat in order to press E.

-- // Functions \\ --
local function GetClosestSeat()
    local SeatsInRange = {}
    for _,seat in pairs(Seats:GetChildren()) do
        local mag = (seat.Position-Character.HumanoidRootPart.Position).Magnitude
        if mag <= RANGE then
            table.insert(SeatsInRange, {Seat = seat, Mag = mag}) -- // Insert into the table because the seat is in range.
        end
    end

    if #SeatsInRange > 0 then -- // If there are seats near the character.
        table.sort(SeatsInRange, function(a,b) -- // Sort the table, so the closest seat is the first index of the table.
            a.Mag < b.Mag 
        end)
        return SeatsInRange[1].Seat -- // Return the closest seat after sorting.
    else
        return nil -- // Return nil if no seats are close.
    end
end

UserInputService.InputBegan:Connect(function(Input, GameProcessed)
    if GameProcessed then return end -- // If player is typing in chat(or other gameProcessed events) then we won't run the code.

    if Input.KeyCode == Enum.KeyCode.E and Humanoid.Sit == false then -- // If E key is pressed and the humanoid is not currently sitting. 
        local ClosestSeat = GetClosestSeat()
        if ClosestSeat ~= nil then -- // If there is a close seat.
            ClosestSeat:Sit(Humanoid)
        end
    end
end)

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 — 4y
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 — 4y
0
I'm too annoying. Sorry. qMrSpooky 20 — 4y
0
Sorry but just add “return” before a.Mag and it should fix it Alphexus 498 — 4y
0
tysm qMrSpooky 20 — 4y
Ad

Answer this question