How can I accomplish a vault system?

So I have a Raycast that detects if a player is up against a wall, that works, the script checks if the wall is shorter than 5 studs (height of an r6 player), that works, the script checks if the player pressed space against the wall, that works, now I need to make the player do a safety vault, how can I do this?

local Player = game.Players.LocalPlayer
local HRP = Player.Character.HumanoidRootPart
local Dist = 3
local WallRay = Ray.new(HRP.Position, HRP.CFrame.LookVector * Dist)
local Wall, HitPosition, Normal, Material = workspace:FindPartOnRay(WallRay, Player.Character)
local UIS = game:GetService("UserInputService")

game:GetService("RunService").RenderStepped:Connect(function()
	repeat
		wait(0.001)
		WallRay = Ray.new(HRP.Position, HRP.CFrame.LookVector * Dist)
		Wall, HitPosition, Normal, Material = workspace:FindPartOnRay(WallRay, Player.Character)
	until Wall
	if Wall then
		if Wall.Size.Y - 5 < 0 then
			UIS.InputBegan:Connect(function(input, gameProcessed)
				if input.KeyCode == Enum.KeyCode.Space then
					--here is where i need to make a vault movement
				end
			end)
		end
	end
end)
1 Like

Sorry for being late!

To answer your question, use RunService.Heartbeat to detect a vaultable part. If the conditions are met for the player to be able to vault over that part, then set a “vaulting” variable to true. Else if the conditions aren’t. met, set it to false. Then just detect when the player presses space, and if the vaulting variable is true they will vault, if it’s false they will not.

Also, the method you’re using is not very efficient for a 2 reasons:

  1. If there are 2 parts on top of each other and one of the parts are shorter than 3 studs, the script thinks that the player can vault.
  2. If the part is going through the ground, but it looks like the player can vault over than it, the script will still think that the player cannot vault.

So, what you should do is this:
Cast a the first ray from the torso forward, then cast a second ray from the head forward (You can use UpVector to make the player be able to vault higher). If the first ray hits, then the part is low enough to vault over. If the second ray hits, then the part is too higher to vault over. If none hit, then the part is too low to vault over. Which means that in order for the player to vault, the first ray has to hit, but the second ray can’t.

There’s an even more advanced way of doing this which allows the player to vault over thinner parts in the air, if you would like to know that method you can message me.

4 Likes