Hello. I need to make it so a player can jump out of a vehicle by pressing the F key instead of the Space key. I was able to successfully do it in this code, right here:
-- If the player pressis the 'F' key, they will jump out of the vehicle
userInputService.InputBegan:Connect(function(inputObject, gameProcessedEvent)
if not gameProcessedEvent then
if inputObject.KeyCode == Enum.KeyCode.F then
local humanoid = character.Humanoid
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
end)
-- Prevent the player from getting out of the vehicle by pressing the space key
contextActionService:BindActionAtPriority("jumpAction", function(_, inputState, _)
if inputState == Enum.UserInputState.End then
print("Player pressed Space, and did not jump out of their seat")
end
end, false, 1, Enum.KeyCode.Space)
-- Run some code down here...
The problem is that now, if the player exits the vehicle they won’t be able to jump using the Space key, and I also need to be able to destroy the script once the player has exited the vehicle. My question is, how can I re-enable / reset the space key so it can behave exactly the way that it did before the player entered the vehicle? It seems like a simple task, but I can’t seem to figure it out.
Try unbinding the space key at the beginning of the script, contextActionService:UnbindAction() and then rebinding it after they exit, contextActionService:BindAction().
Another alternative (I don’t know if it will work) is after you BindActionAtPriority() and run the code to BindActionAtProirity() but in reverse.
Hope this helped, I’m not the best at scripting, so sorry if this doesn’t work.
I ended up solving this by making another script with a bindable event in it that the car script can fire when the player exits the vehicle, and then when that bindable event is fired, it binds it’s own jump function.
local players = game:GetService("Players")
local contextActionService = game:GetService("ContextActionService")
function rebind(inputState)
if inputState == Enum.UserInputState.Begin then
if players.LocalPlayer.Character.Humanoid.FloorMaterial ~= Enum.Material.Air then
players.LocalPlayer.Character.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
return Enum.ContextActionResult.Pass
end
end
end
script.Rebind.Event:Connect(function()
contextActionService:UnbindAction("jumpAction")
contextActionService:BindActionAtPriority("jumpAction", function(_, inputState, _)
rebind(inputState)
end, false, 1, Enum.KeyCode.Space)
end)
Someone please tell me if there is a better way to do this.