How do I prevent spam key input (vaulting system)

Hello, I’m new to script and currently making a vaulting system. I wonder how I prevent player from spamming “Space” spam play animation when vaulting over an object.

Here is my local script:

   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 UIS = game:GetService("UserInputService")
   local vaultavail = true
   local prt = false

    while game:GetService("RunService").RenderStepped:Wait() do
local r = Ray.new(HRP.Position, HRP.CFrame.LookVector * 7 + HRP.CFrame.UpVector * -5)
local part = workspace:FindPartOnRay(r,char)

if part and vaultavail then
	if part.Name == "Vault" then
		prt = true
	else
		prt = false
		if Hum.FloorMaterial ~= Enum.Material.Air then
			UIS.InputBegan:Connect(function(Input,isTyping)
				if Input.KeyCode == Enum.KeyCode.Space and prt == true then
			vaultavail = 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 * 3.3 + Vector3.new(0,4,0)
			Hum.WalkSpeed = 0
			wait (0.1)
			CA:Play()
			game.Debris:AddItem(Vel, .15)
			wait(1)
			vaultavail = true
			end
			end)
		end
	end
end
end

you should diconnect your connection if its not used anymore.

local connection
connection = UIS.InputBegan:Connect(function(Input,isTyping)
			if Input.KeyCode == Enum.KeyCode.Space and prt == true then
                                connection:Disconnect()
				vaultavail = 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 * 3.3 + Vector3.new(0,4,0)

				Hum.WalkSpeed = 0
				wait (0.1)
				CA:Play()

				game.Debris:AddItem(Vel, .15)
				wait(1)
				vaultavail = true
			end
		end)

you should just do this instead of a RenderStepped loop

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")) -- shouldnt use Hum:LoadAnimation cause its deprecated.
local UIS = game:GetService("UserInputService")
local vaultavail = true
local prt = false

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then
		return
	end
	if input.KeyCode ~= Enum.KeyCode.Space or not prt then
		return
	end

	local ray = Ray.new(HRP.Position, HRP.CFrame.LookVector * 7 + HRP.CFrame.UpVector * -5)

	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = { char }

	local result = workspace:Raycast(ray.Origin, ray.Direction, raycastParams)
	if not result or not vaultavail then
		return
	end

	vaultavail = 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 * 3.3 + Vector3.new(0, 4, 0)
	Hum.WalkSpeed = 0

	task.wait(0.1)
	CA:Play()
	game.Debris:AddItem(Vel, 0.15)
	task.wait(1)
    vaultavail = true
end)

Use events instead of loops for most cases.

hello thank you for replied to my problem. I’m still new to scripts and I wonder the part where I shouldnt use Hum:LoadAnimation, If i shouldnt use what can i do?

you should use this instead Animator | Documentation - Roblox Creator Hub

i see… Thank you for helping me, really appreciate it!

btw try not to use wait() use task.wait() instead. wait() is deprecated too.

2 Likes

wait now i have new problem. Your new code doesnt include “if part.Name == “Vault” then”. Basically im creating a vaulting over obstacle (Specific obstacle). For example, i set object in name with “Vault” and make the local system detect if any object named “vault” in the game, I can jump over it.

when player press input key, it play animation and jump over object that’s named “Vault” or sth. However, facing a problem where player can spam it. I want to make a cool down like wait 1,2 second before they can press input key again.

this is the best I can come up with the info I have. but you should try to figure it out yourself.

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")) -- shouldnt use Hum:LoadAnimation cause its deprecated.
local UIS = game:GetService("UserInputService")

local vaultavail = true
local prt = false
local partName = "Vault"
local cooldown = 2

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = { char }

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent or not vaultavail then
		return
	end

	if input.KeyCode ~= Enum.KeyCode.Space or not prt then
		return
	end

	local ray = Ray.new(HRP.Position, HRP.CFrame.LookVector * 7 + HRP.CFrame.UpVector * -5)

	local result = workspace:Raycast(ray.Origin, ray.Direction, raycastParams)
	if not result or result.Instance.Name ~= partName then
		return
	end

	vaultavail = 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 * 3.3 + Vector3.new(0, 4, 0)
	Hum.WalkSpeed = 0

	task.wait(0.1)

	CA.Ended:Once(function()
		task.wait(cooldown)
		vaultavail = true
	end)

	CA:Play()
	task.delay(0.15, Vel.Destroy, Vel) -- destroys BV after 0.15 seconds

	-- task.wait(cooldown + CA.Length * CA.Speed) use this if CA.Ended does not work out
	-- vaultavail = true
end)

hopefully this helps :slight_smile:

alright thank you for helping me. Really appreciate it!