I’m trying to make a script in ROBLOX Studio, where, when you hit the ground, a landing animation plays. I think I have everything right, except for the Humanoid’s state. The script just keeps looping the animation when I test it out. Here’s a demonstration video, and the code.
while true do
wait()
if script.Parent.Humanoid:GetState(Enum.HumanoidStateType.Landed) then
local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=2524640496"
local playAnim = script.Parent.Humanoid:LoadAnimation(anim)
playAnim:Play()
wait(1)
anim:Destroy()
end
end
If you could help point out the flaws in my code, it might help me to better fix the code so it works as intended.
5 Likes
Did you have the animation on loop? That could possibly be it. (Try disabling loop)
No, it’s not on loop. I made sure it was disabled.
Doesnt that loop things? Probs should remove that.
If I do, everything stops working.
Try referring to this:
(They used connect instead of While True Do since it loops)
newState == Enum.HumanoidStateType.Landed then?
1 Like
Alright, let me try that recommendation.
if script.Parent.Humanoid.StateChanged(Enum.HumanoidStateType.Landed) then
local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=2524640496"
local playAnim = script.Parent.Humanoid:LoadAnimation(anim)
playAnim:Play()
wait(1)
anim:Destroy()
end
I got this, and it only plays once, and in midair, then never works again.
Hmmm, maybe just have the animation in the character and play it by locating it and doing playAnim:Play() whenever it lands.
An example of them using jump with landed events:
local character = script.Parent
local primaryPart = character.PrimaryPart
-- create particles
local particles = Instance.new("ParticleEmitter")
particles.Size = NumberSequence.new(1)
particles.Transparency = NumberSequence.new(0, 1)
particles.Acceleration = Vector3.new(0, -10, 0)
particles.Lifetime = NumberRange.new(1)
particles.Rate = 20
particles.EmissionDirection = Enum.NormalId.Back
particles.Enabled = false
particles.Parent = primaryPart
local humanoid = character:WaitForChild("Humanoid")
local isJumping = false
-- listen to humanoid state
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Jumping then
if not isJumping then
isJumping = true
particles.Enabled = true
end
elseif newState == Enum.HumanoidStateType.Landed then
if isJumping then
isJumping = false
particles.Enabled = false
end
end
end)
1 Like
How would I go about doing that?
This is a little hard for me to put into the script, like to know where to put it and what to remove.
Insert an animation into the character. You could probably do it by putting an animation in StarterCharacterScripts and inserting it.
elseif newState == Enum.HumanoidStateType.Landed then
if isJumping then
isJumping = false
particles.Enabled = false
end
end
end)
The main functions of it landing ^^^
(Just an example)
if script.Parent.Humanoid.StateChanged(Enum.HumanoidStateType.Landed) then
local anim = script.Parent.Animation
local playAnim = script.Parent.Humanoid:LoadAnimation(anim)
playAnim:Play()
wait(1)
anim:Destroy()
end
Now it doesn’t even work at all.
Could you show your explorer of where you put the animation at?
(also, make sure the id is actually in the animation)
Basically what @3rdhoan123 is saying, except with your frame of reference:
local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://2524640496"
local playAnim = script.Parent.Humanoid:LoadAnimation(anim)
script.Parent.Humanoid.StateChanged:Connect(function(_, state)
if state == Enum.HumanoidStateType.Landed then
playAnim:Play()
end
end)
20 Likes
Wow, it actually works! Thanks a lot! I’ll look into the script to help me understand it better.
1 Like
Remember to mark his as a solution if it worked.