So I recently tried to code a function for crouching in a game. For some reason, the script does not play when i press “C”. Anyone knows why this is happening?
local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = game.WaitForChild("Humanoid")
local isRunning = false
local Animation = Humanoid:LoadAnimation(script:WaitForChild("Animation"))
UserInputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.C then
if not isRunning then
isRunning = true
Animation:Play()
Humanoid.WalkSpeed = 10
else
Animation:Stop()
Humanoid.WalkSpeed = 16
isRunning = false
end
end
end)
Do you own the animation OR is the game owned by a group? (And is the Priority set to ACTION) Also Humanoid:LoadAnimation() is depracated use Humanoid:WaitForChild("Animator"):LoadAnimation()
local UserInput = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local IsCrouching = false
local Animation = script:WaitForChild("Animation")
local Track = Humanoid:LoadAnimation(Animation)
UserInput.InputBegan:Connect(function(Key, Processed)
if Processed then return end
if Key.KeyCode == Enum.KeyCode.C then
IsCrouching = not IsCrouching
if IsCrouching then
Humanoid.WalkSpeed = 10
Track:Play()
else
Humanoid.WalkSpeed = 16
Track:Stop()
end
end
end)
I tested this with one of Roblox’s animations (Cartoony Run) and it worked.