Detecting a ledge

I have watched a video about implementing a vault system into my game, by LTMaiko. I took his script, and edited it so that the player would vault over the part when they press space. The code I used is here:

local plr = game:GetService(“Players”).LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local HRP = char:WaitForChild(“HumanoidRootPart”)
local Hum = char:WaitForChild(“Humanoid”)
local CA = Hum:LoadAnimation(script:WaitForChild(“ClimbAnim”))
local vaultSound = script.Vault
local UIS = game:GetService(“UserInputService”)

local ledgeavail = true

UIS.InputBegan:Connect(function(key)
local r = Ray.new(HRP.Position, HRP.CFrame.LookVector * 7 + HRP.CFrame.UpVector * -5)
local part = workspace:FindPartOnRay(r,char)
if part and ledgeavail and key.KeyCode == Enum.KeyCode.Space then
if part:FindFirstChild(“CanVault”) then
if Hum.FloorMaterial ~= Enum.Material.Air then
ledgeavail = false
local Vel = Instance.new(“BodyVelocity”)
Vel.Parent = HRP
Vel.Velocity = Vector3.new(0,0,0)
Vel.MaxForce = Vector3.new(1,1,1) * math.huge
Vel.Velocity = HRP.CFrame.LookVector * 10 + Vector3.new(0,15,0) + HRP.Velocity / 1.3
CA:Play(0.1, 1, 1)
game.Debris:AddItem(Vel, .15)
vaultSound:Play()
wait(0.75)
ledgeavail = true
end
end
end
end)

There are no bugs or errors with this script. The only problem is that I have to manually insert a value called “CanVault” inside of the part so that the script knows that it can vault over it. I want my script to detect a ledge so that my player can vault over it.

What I did was make a raycast that started from the head forward and another that started from the torso and if the raycast of the torso collided with a part and the raycast of the head did not, then I made the character jump and each raycast with 2 of distance

4 Likes

Yea I figured that already, I probably shouldn’t keep this post without a solution so ima just marks yours as it lol

2 Likes

can you teach me how you made the raycasts, i am currently trying to learn.

Basically how it works is:
Have a RunService.Heartbeat event that checks for 2 rays - one from the head forward, one from the torso forward. If the head raycast is nil, and the torso raycast is not, set a CanVault variable to true. Else set it to false.

Then have a UserInputService.InputBegan to detect when the player presses spacebar. If CanVault = true, use BodyVelocity to launch the player forward and do an animations to make them vault. If CanVault = false then don’t do it.

1 Like

Holy, Thank you so much, honestly. I’ve been trying to make a vault system. For. So. Long.