Sorry for my bad grammar, English is not my first language
-
What do you want to achieve?
When I hold shift or C the animation will play -
What is the issue?
The animation plays before I wanted it to -
What solutions have you tried so far?
I have tried to look up on API Reference on roblox
The code(Local script):
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild('Humanoid')
local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = ""
local RAnimation = Humanoid:LoadAnimation(RunAnimation)
local CrouchAnimation = Instance.new('Animation')
CrouchAnimation.AnimationId = ""
local CAnimation = Humanoid:LoadAnimation(RunAnimation)
local CrouchIdleAnimation = Instance.new('Animation')
CrouchIdleAnimation.AnimationId = ""
local CIAnimation = Humanoid:LoadAnimation(RunAnimation)
local ContextActionService = game:GetService("ContextActionService")
local CrouchingSpeed = 5
local RunSpeed = 20
local DefultSpeed = 10
local Holding = false
local Running = false
local Crouching = false
local function Run()
if Player:FindFirstChild("Crouching") ~= nil and Crouching == false then
if Holding == false then
Holding = true
RAnimation:Play()
print("Running")
Humanoid.WalkSpeed = RunSpeed
Running = true
else
print("Stopped running")
Running = false
Holding = false
RAnimation:Stop()
Humanoid.WalkSpeed = DefultSpeed
end
end
end
local function Crouch()
if Player:FindFirstChild("Crouching") ~= nil and Running == false then
if Holding == false then
Crouching = true
Holding = true
print("crouching")
Player.Crouching.Value = true
CAnimation:Play()
Humanoid.WalkSpeed = CrouchingSpeed
else
Crouching = false
Holding = false
print("stoppped crouch")
Player.Crouching.Value = false
CAnimation:Stop()
Humanoid.WalkSpeed = DefultSpeed
end
end
end
ContextActionService:BindAction("Run", Run, true, Enum.KeyCode.LeftShift)
ContextActionService:SetTitle("Run", "Run")
ContextActionService:SetPosition("Run",UDim2.new())
ContextActionService:BindAction("Crouch", Crouch, true, Enum.KeyCode.C)
ContextActionService:SetTitle("Crouch", "Crouch")
ContextActionService:SetPosition("Crouch",UDim2.new())
Humanoid.StateChanged:Connect(function(OldState, NewState)
if NewState == Enum.HumanoidStateType.RunningNoPhysics then
CIAnimation:Play()
print("idle")
else
print("not idle")
CIAnimation:Stop()
end
end)
I know local script is a bad idea for this but my game won’t be popular so I don’t really care