How to add a key input in this script

What do you want to achieve?
I want to add a key input, so that script only works when pressing “Space” key in keyboard… I have tried some stuff but i couldn’t get it to work.

Also is it possible to change the studs height to 4 or 5 studs?

Thanks in advance.

What solutions have you tried so far?
Tried looking in some Devforum posts and tried some stuff, but i wasn’t succesful…

Code:

game:GetService("RunService").Heartbeat:Connect(function()
	local rayF = workspace:Raycast(char.HumanoidRootPart.CFrame.Position, char.HumanoidRootPart.CFrame.LookVector * 2 + char.HumanoidRootPart.CFrame.UpVector * -1.25, raycastParams)
	local rayU = workspace:Raycast(char.HumanoidRootPart.CFrame.Position, char.HumanoidRootPart.CFrame.LookVector * 3 + char.HumanoidRootPart.CFrame.UpVector , raycastParams)


	local ray = rayF


	if ray ~= nil and rayU == nil and char.Humanoid.FloorMaterial ~= Enum.Material.Air then
		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 * 15 + Vector3.new(0,25,0)
		vaultAnim:Play()
		wait(0.15)
		Vel:Destroy()
	end
end)
1 Like

Tried with UserInputService, but i think that i was using it in a wrong way, since i couldn’t make it work…

Either A: Wrap your existing code in a function and call it when user presses space
or B: Have a boolean value change when user presses space. Then only run the code when the value is true.

Use UserInputService or ContextActionService.

Examples:
A:

function foo() 
 print("your code");
end
UserInputService.InputBegan:Connect(foo());

B:

local bool = false;
Runservice.RenderStepped.Heartbeat:Connect(function()
 if bool == true then
  print("your code"); 
end
end)

// handle input here
// im not going to write it because i dont want to lol
1 Like

I got it to work. You need to increase how far the ray shoots, because the ray returns nil since it’s not reaching any parts.

Ray distance example:

local down = (CF.LookVector * 3) + (CF.UpVector * -8) -- 8 is just an example.
local rayF = workspace:Raycast(CF.Position, down,  params)

UserInputService:

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent or input.KeyCode ~= Enum.KeyCode.Space then
		return
	end
	
	-- Raycast here.
end)

If you’re having trouble with your vault animation, maybe disabling the player from being able to jump will help. You can do this by setting the character’s Humanoid's JumpHeight or JumpPower property to zero.

1 Like

So would you suggest me changing Ray Distance for the one you posted? Like this?

Old:

local rayF = workspace:Raycast(char.HumanoidRootPart.CFrame.Position, char.HumanoidRootPart.CFrame.LookVector * 2 + char.HumanoidRootPart.CFrame.UpVector * -1.25, raycastParams)
local rayU = workspace:Raycast(char.HumanoidRootPart.CFrame.Position, char.HumanoidRootPart.CFrame.LookVector * 3 + char.HumanoidRootPart.CFrame.UpVector , raycastParams)

New:

local down = (CF.LookVector * 3) + (CF.UpVector * -8) -- 8 is just an example.
local rayF = workspace:Raycast(CF.Position, down,  params)

You should change the ray distance for both of the rays you are shooting.

Tried changing the ray distance but seems like i can’t make it work

OK. Here is the script that I made using the code you provided. Try this. Also, I put the script inside of StarterCharacterScripts.

-- // VARIABLES
-- / Services
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

-- / Character
local char = script.Parent
local HRP = char:WaitForChild("HumanoidRootPart")

local Humanoid : Humanoid = char:WaitForChild("Humanoid")
Humanoid.JumpHeight = 0

-- / Inputs
local KEY = Enum.KeyCode.Space

-- // MAIN
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent or input.KeyCode ~= KEY then
		return
	end
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {char}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local CF = HRP.CFrame
	
	local down = CF.LookVector * 3 + CF.UpVector * -8
	local up =  CF.LookVector * 2 + CF.UpVector * 8
	
	local rayF = workspace:Raycast(CF.Position, down, raycastParams)
	local rayU = workspace:Raycast(CF.Position, up, raycastParams)
	
	local ray = rayF

	if ray ~= nil and rayU == nil and Humanoid.FloorMaterial ~= Enum.Material.Air then
		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 * 15 + Vector3.new(0,25,0)
		--vaultAnim:Play()
		wait(0.15)
		Vel:Destroy()
	end
end)
runservice = game:GetService("RunService")
UIS = game:GetService("UserInputService")
key = Enum.KeyCode.Space

runservice.RenderStepped:Connect(function() ---renderstepped is more efficient than heartbeat
	local rayF = workspace:Raycast(char.HumanoidRootPart.CFrame.Position, char.HumanoidRootPart.CFrame.LookVector * 2 + char.HumanoidRootPart.CFrame.UpVector * -1.25, raycastParams)
	local rayU = workspace:Raycast(char.HumanoidRootPart.CFrame.Position, char.HumanoidRootPart.CFrame.LookVector * 3 + char.HumanoidRootPart.CFrame.UpVector , raycastParams)
---- we keep these variable above on the renderstepped because we are gonna be using them often
UIS.InputBegan:Connect(function(input)
if input.KeyCode == key then --- key is referenced above in a local variable
local ray = rayF


	if ray ~= nil and rayU == nil and char.Humanoid.FloorMaterial ~= Enum.Material.Air then
		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 * 15 + Vector3.new(0,25,0)
		vaultAnim:Play()
		wait(0.15)
		Vel:Destroy()
	end
end)

lmk if it works.

@Ovibion @Krexerius None of those worked as intended, Vault animation keeps playing even if you jump out of a vault zone.

The script i provided works fine, but the only 2 problems i have are that i can’t set “Space” as the Key input and that i’m not sure about how to limit the vault action to only parts that are 4 or 5 studs of height.

oh!! alright i get what you need. Give me a bit and ill rewrite something more functional for you!

So, are you not getting any information from your Output window? Try adding print() statements throughout your code to see which steps are being completed and which steps are not.

I am not working with an animation, so I do not know how to solve that issue.

thats your code ^

now

UIS = game:GetService("UserInputService")
key = {Enum.KeyCode.Space}

function Vaulting()
	local rayF = workspace:Raycast(char.HumanoidRootPart.CFrame.Position, char.HumanoidRootPart.CFrame.LookVector * 2 + char.HumanoidRootPart.CFrame.UpVector * -1.25, raycastParams)
	local rayU = workspace:Raycast(char.HumanoidRootPart.CFrame.Position, char.HumanoidRootPart.CFrame.LookVector * 3 + char.HumanoidRootPart.CFrame.UpVector , raycastParams)


	local ray = rayF


	if ray ~= nil and rayU == nil and char.Humanoid.FloorMaterial ~= Enum.Material.Air then
		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 * 15 + Vector3.new(0,25,0)
		vaultAnim:Play()
		wait(0.15)
		Vel:Destroy()
	end
end)

UIS.InputBegan:Connect(function(input))
if input.KeyCode == key then
Vaulting()
end
end)

I wouldnt really know how to specifically make it work for a specific stud height, i guess you would have to make it work globally (for any height)