Keep key pressed to activate

I have this script, and I want to transform it into the same, but with the difference that to activate the key, you have to hold it down.

local UserInput = game:GetService("UserInputService")
local Running = false
local WalkSpeed = script.Parent.Parent.Parent.Parent.Parent:WaitForChild("WalkSpeed").Value

script.Parent.Parent.Parent.Parent.Parent.WalkSpeed:GetPropertyChangedSignal("Value"):Connect(function()
	WalkSpeed = script.Parent.Parent.Parent.Parent.Parent.WalkSpeed.Value
end)

local KeyText = "C"

repeat wait() until game.Players.LocalPlayer

local AgacharseKey = game.Players.LocalPlayer:WaitForChild("Keys"):WaitForChild("AgacharseKey")

AgacharseKey:GetPropertyChangedSignal("Value"):Connect(function()
	KeyText = AgacharseKey.Value
end)

UserInput.InputBegan:Connect(function(key)
	if (key.KeyCode.Name == KeyText) then
		if not Running then
			Running = true
			--poner animacion
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = WalkSpeed
			game.Players.LocalPlayer.Character.Animate.idle.Animation1.AnimationId = "rbxassetid://6567685317"
			game.Players.LocalPlayer.Character.Animate.idle.Animation2.AnimationId = "rbxassetid://6567685317"
			game.Players.LocalPlayer.Character.Animate.run.RunAnim.AnimationId = "rbxassetid://6567706473"
			game.Players.LocalPlayer.Character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://6567706473"
		else
			--desactivar animacion
			Running = false
			game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = WalkSpeed
			game.Players.LocalPlayer.Character.Animate.idle.Animation1.AnimationId = "rbxassetid://2510196951"
			game.Players.LocalPlayer.Character.Animate.idle.Animation2.AnimationId = "rbxassetid://2510197257"
			game.Players.LocalPlayer.Character.Animate.run.RunAnim.AnimationId = "rbxassetid://913376220"
			game.Players.LocalPlayer.Character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://913402848"
		end
	end
end)
1 Like

I presume that you want to tweak this script to smoothly transition into a running state?

You can use IsKeyDown to check if a user is holding a key,
with a loop of course.

local UserInputService = game:GetService("UserInputService")
local Heartbeat = game:GetService("RunService").Heartbeat
local KeyName = "E"

UserInputService.InputBegan:Connect(function(input, processed)
	if input.KeyCode.Name == KeyName then
		while true do
			if UserInputService:IsKeyDown(Enum.KeyCode[KeyName]) then
				-- // Is Holding
			else
				break
			end
			Heartbeat:Wait()
		end
	end
end)
1 Like

Try this code:

local Player = game.Players.LocalPlayer
local UserInput = game:GetService("UserInputService")
local RunService = game:GetService("RunService") -- To avoid while-wait loops
local Running = false
local WalkSpeed = script.Parent.Parent.Parent.Parent.Parent:WaitForChild("WalkSpeed").Value -- Please remove the spam .Parent :(

script.Parent.Parent.Parent.Parent.Parent.WalkSpeed:GetPropertyChangedSignal("Value"):Connect(function()
	WalkSpeed = script.Parent.Parent.Parent.Parent.Parent.WalkSpeed.Value
end)

local KeyText = "C"
local KeyDown = false
local KeyPressedTimestamp = 0
local TimeDownRequirement = 1 -- How long before the thing happens?

--repeat wait() until game.Players.LocalPlayer -- This is redundant

local AgacharseKey = Player:WaitForChild("Keys"):WaitForChild("AgacharseKey")

AgacharseKey:GetPropertyChangedSignal("Value"):Connect(function()
	KeyText = AgacharseKey.Value
end)

local function PonerAnimacion() -- Thought I'd use your language here :P
    local Character = Player.Character
    local Humanoid = Character.Humanoid
    local AnimateScript = Character.Animate

    Humanoid.WalkSpeed = WalkSpeed
	AnimateScript.idle.Animation1.AnimationId = "rbxassetid://6567685317"
	AnimateScript.idle.Animation2.AnimationId = "rbxassetid://6567685317"
	AnimateScript.Animate.run.RunAnim.AnimationId = "rbxassetid://6567706473"
	AnimateScript.Animate.walk.WalkAnim.AnimationId = "rbxassetid://6567706473"
end

local function DesactivarAnimacion()
    local Character = Player.Character
    local Humanoid = Character.Humanoid
    local AnimateScript = Character.Animate

    Humanoid.WalkSpeed = WalkSpeed -- Btw, is this a mistake?
	AnimateScript.idle.Animation1.AnimationId = "rbxassetid://2510196951"
	AnimateScript.idle.Animation2.AnimationId = "rbxassetid://6567685317"
	AnimateScript.Animate.run.RunAnim.AnimationId = "rbxassetid://913376220"
	AnimateScript.Animate.walk.WalkAnim.AnimationId = "rbxassetid://913402848"
end

UserInput.InputBegan:Connect(function(key)
	if (key.KeyCode.Name == KeyText) then
        KeyPressedTimestamp = tick() -- Store the time for when the key was pressed
        KeyDown = true -- The key is down!
	end
end)

UserInput.InputEnded:Connect(function(key) -- Detect when key is lifted
    if (key.KeyCode.Name == KeyText) then
        KeyDown = false -- The key is lifted!
    end
end)

RunService.Heartbeat:Connect(function() -- A forever-loop
    if not Running and KeyDown and tick() - KeyPressedTimestamp >= TimeDownRequirement then -- Only run if not sprinting AND if the key is down AND the key has been down long enough
        Running = true
        PonerAnimacion()
    elseif Running and not KeyDown then -- Stop running if the user is already running AND if the key is lifted
        Running = false
        DesactivarAnimacion()
    end
end)

Comments are included to help you understand it a bit more :slight_smile:

2 Likes

Curiously, it no longer works, try again and it does not run.

What is the output? That may help find the problem.

Well, no problem, I already fixed it. I realized that one part of the script was not working, and it was the part of local function SetAnimation() and the other. The rest if it worked and well at the end I put the script where it detects when pressing the script, and that’s how it worked

1 Like