I tried making an crouch script, that when a player presses a specific button, he crouches, and when he lets go, then he goes to standing up again.
the animation only plays once when I press the button even though I didn’t let go
Tried looping the animation but it was a bad idea since I made the animation from standing up to crouching, so if I loop it, he keeps standing up and down.
local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:FindFirstChild("Humanoid")
local uis = game:GetService("UserInputService")
local Animator = hum:WaitForChild("Animator")
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.S then
local animation = Instance.new("Animation")
local animDown = game.Workspace.Animations.crouch
local animDownTrack = Animator:LoadAnimation(animDown)
animDownTrack:Play()
hum.WalkSpeed = 10
end
end)
uis.InputEnded:Connect(function()
local animation = Instance.new("Animation")
local animDown = game.Workspace.Animations.crouch
local animDownTrack = Animator:LoadAnimation(animDown)
animDownTrack:Stop()
hum.WalkSpeed = 16
end)
Well Just simply playing one animation, isn’t going to work as you want it to. I recommend making a function that tells if a player is moving or not and plays an idle or movement script, dependent on that.
I’ve done that before, so maybe I could help some
.
Also why aren’t you detecting if the input of the USI is a Enum.InputState.Begin
Don’t we just have to check if the player pressed a button, he just plays an animation that keeps going? Maybe we should seperate the start up of the animation (the player crouching from an standing state) and then play another crouch idle animation and keep the low walkspeed. Do you know how I can make that?
So first I would play a separate animation for the character going down to crouch. After that, I would make a looping animation for idling/walking that would play until the player gets up again.
Ok lets say we got the start up played after the player pressed the button, how can I start the other loop animation right after the start up animation stopped?
Ah, my short animations look weird but here is part of the script that will play the getting down animation, then play the looping one.
local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:FindFirstChild("Humanoid")
local uis = game:GetService("UserInputService")
local Animator = hum:WaitForChild("Animator")
local CrouchAnim = script:WaitForChild("Crouch")
local CrouchIdleAnim = script:WaitForChild("CrouchIdle")
local CrouchMoveAnim = script:WaitForChild("CrouchMove")
local GetDownAnimation = Animator:LoadAnimation(CrouchAnim)--- Put in that animation Id in here
local LoopMoveAnimation = Animator:LoadAnimation(CrouchMoveAnim)--- Put in a crouching move animation id here
local LoopIdleAnimation = Animator:LoadAnimation(CrouchIdleAnim)--- Put in a Idle animation Id for crouching here
----Imagine your function here to begin the crouch
GetDownAnimation:Play()
GetDownAnimation.Stopped:Connect(function()
LoopIdleAnimation:Play()
end)
Here is a video showing that loop and get-down animation, if you want to see it. robloxapp-20220907-1751235.wmv (856.9 KB)
You’ll have to copy and paste that start animation to your current function. Also, you’ll have to add those animation instances into the script and put in an Id for them.