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)
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
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