Now I assume this is scripting with a key down script. I have not been able to find how this was done. Can someone let me know. Thanks from John.
Hereâs an example using the ContextActionService, which will allow you to bind actions cross-platform a little easier:
function SitRequest(name, state, input)
if (state ~= Enum.UserInputState.Begin) then return end
-- Check if close enough to vehicle.
-- If so, sit in the seat.
-- Example:
local player = game.Players.LocalPlayer
local seat = someSeat
if (player.Character and player:DistanceFromCharacter(seat.Position) < 30) then
seat:Sit(player.Character.Humanoid)
end
end
game:GetService("ContextActionService"):BindAction(
"Sit",
SitRequest,
false,
-- Inputs to listen to:
Enum.KeyCode.E,
Enum.KeyCode.ButtonX
)
Note that the example above isnât necessarily game-ready. You might want a single binding that will do this for all the vehicles in your game. In that instance, you could loop through all the vehicles to see which one is closest. That would be a passive system. If you want an active system, you might even keep a continuous record of which vehicle is closest, and then use that vehicle in the SitRequest
function. But that would require a lot more code. You could technically use FindPartsInRegion3
too, but I would find that a bit overkill, since checking distances between objects is really easy.
Ok thanks! its Crazyman32 I enjoyed your Ro Port Tycoon!
To make it easier to check the âVehiclesâ and their distance use CollectionService for the parts that contain the BillboardGui (if youâre using one) or a main part of the vehicle. You can use this tutorial:
and re-do some of the code to fit with what youâre trying to do. Such as detect the distance, then the input. It can all be done locally, as it should be when working with User Inputs, and if youâre working with FilteringEnabled (which I highly recommend )
If you use the following example, the localscript should be put in StarterCharacterScripts, and change âInputTriggerBrickâ to whatever the tag name is.
----- Services -----
local cs = game:GetService('CollectionService')
local cas = game:GetService('ContextActionService')
local uis = game:GetService('UserInputService')
local rs = game:GetService('RunService')
----- Player Stuff -----
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
---- Variables -----
local data = {}
local bricks = {}
local canInput = false
local closestPart
----- Functions -----
local function sitRequest()
--[[
INSERT CRAZYMAN32'S CODE HERE
--]]
if closestPart then
if closestPart.BrickColor == BrickColor.new("Really red") then
closestPart.BrickColor = BrickColor.new("Medium stone grey")
elseif closestPart.BrickColor == BrickColor.new("Medium stone grey") then
closestPart.BrickColor = BrickColor.new("Really red")
end
end
end
local function closestTrigger()
for _, part in pairs(cs:GetTagged("InputTriggerBrick")) do
local distance = (part.Position - char.PrimaryPart.Position).magnitude
local closePartDistance
if closestPart then
closePartDistance = (closestPart.Position - char.PrimaryPart.Position).magnitude
end
if distance <= 5 then
print('Close Part: ', part.Name)
if uis.TouchEnabled then
part.InputGui.InputText.Text = "[ TAP ]" --- InputGui is the billboard INSIDE the trigger part.
elseif uis.MouseEnabled then
part.InputGui.InputText.Text = "[ E ]" --- InputText is a TextLabel inside InputGui
end
local gamepads = uis:GetConnectedGamepads()
if #gamepads >= 1 then
part.InputGui.InputText.Text = " [ X ]"
end
part.InputGui.Enabled = true
canInput = true
closestPart = part
print('Can Input: ', canInput)
elseif distance > 5 then
part.InputGui.Enabled = false
if closePartDistance then
if closePartDistance < distance then
return
end
end
canInput = false
closestPart = nil
print('Can Input: ', canInput)
end
end
end
----- Code -----
rs.RenderStepped:connect(function()
closestTrigger()
end)
uis.InputBegan:connect(function(input, gameEvent)
if gameEvent then return end
if not canInput then return end
if input.KeyCode == Enum.KeyCode.E then
sitRequest()
elseif input.KeyCode == Enum.KeyCode.X then
sitRequest()
end
end)
uis.TouchTap:connect(function()
if not canInput then return end
sitRequest()
end)
Thanks guys!
haha
Donât forget to mark the thread as solved so others know!
Well hey, itâs true!
If you want, I could also make a short tutorial video on how to do this. I would utilize the CollectionService as @Eventive mentioned. I feel like this would benefit a lot of users. It could be a single LocalScript that could be dropped into the StarterCharacterScripts folder.
I bet it could benefit new users as well! I say if you have the time, go for it.
I did it. Itâs 40 minutes long omg. It doesnât cover mobile support or changing the label dynamically though. Oops. Whatever, I guess itâs a good start.
Edit: Made a shorter version that just summarizes the code:
Iâll check it out once itâs done!
Thanks!