Play animation upon landing?

I know that there are similar threads to this, but only one works and it’s not fitting my gameplay.

When the character lands, I want them to play an animation. HOWEVER, let’s say they fall for about 4 or 5 seconds, the animation plays. There’s a similar thread to this, which is here.
Ever since studio has been updated with brand new code, I’m completely stumped on what I should do.

I already have a script that I’ve been using to test the animation, and it’s stellar, but I want the animation to play AFTER a couple of seconds.
The script I’ve been using is a local script and used in StarterCharacterScripts. Here’s the code.

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://9555789222"
local playAnim = script.Parent.Humanoid:LoadAnimation(anim)

script.Parent.Humanoid.StateChanged:Connect(function(_, state)
	if state == Enum.HumanoidStateType.Landed then
		playAnim:Play()
	end
end)

What do I do to turn this script to make the animation play after falling for an amount of seconds? If someone could rewrite the code and teach me what’s-what, I’d greatly appreciate that. Thank you!

I think there’s a way to detect if the player has been holding that condition for over 5 seconds. Again, I’m not fully sure.

1 Like

There is, but even I can’t figure that out! New code stumps me a lot.

Let’s see if I can rewrite it…

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://9555789222"
local playAnim = script.Parent.Humanoid:LoadAnimation(anim)

script.Parent.Humanoid.StateChanged:Connect(function(_, state)
	if state == Enum.HumanoidStateType.Landed then
        
        if wait(5) then
    		playAnim:Play()
        else
        -- do nothing
        end
	end
end)
1 Like
local oldTime 

script.Parent.Humanoid.StateChanged:Connect(function(_, state)
    if state == Enum.HumanoidStateType.Jumping then -- Player Jumped, set time for tick
       oltTime = tick()
elseif state == Enum.HumanoidStateType.Landed then -- Player landed, did they fall for 5 seconds?
        local new = tick()
		if math.abs(oldTime - new) > 5 then
           playAnim:Play()
        end
	end
end)

You can try to keep track of them jumping and when they land compare the time when they jumped to when they landed

3 Likes

You tried (and I really appreciate that!) but when I went to go test it, the animation played after landing lol. Here’s an example of it.

Thanks a lot! But there’s a lot of errors in the script… Where am I supposed to put this code at?

You know that, this actually sounds like it works better IMO.

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://9555789222"
local playAnim = script.Parent.Humanoid:LoadAnimation(anim)

freefallTime = 5
local RunService = game:GetService("RunService")

local start = os.clock()

script.Parent.Humanoid.StateChanged:Connect(function(_, state)
	if state == Enum.HumanoidStateType.FreeFalling then

        if os.clock() - start < freefallTime then
            if state == Enum.HumanoidStateType.Landed then
		      playAnim:Play()
            end
        end
	end
end)

Though, @AquaBlackChan you can try to see if this works.

Edit: Taken some code from another post on detecting if time passed, let me know if it worked or not.

1 Like

You replace your event with it, i had a mistake in the script where i subtracted the old time(a constant now) to the new time(a larger number as of landing) and it will always come out as a negative number, you can try to just get it’s absolute value so that the animation only plays after 5 seconds of falling

1 Like

Relooking at this, this might not work with how it’s setup

1 Like

Yeah, I tried testing some stuff with it, like making a higher platform and trying to jump off at a certain height, even with following the advice from @TenBlocke it still didn’t really work. ):

1 Like

I’ve edited some of the code. I used another post that was talking about how to detect if 10 seconds have passed.

1 Like

Tried your edited script, and although I really do appreciate the help the both of you are doing, but it’s still not working. Tried to see if it was because I was jumping or if it didn’t work unless I fell off a ledge. Here’s another video by trying out @edozune edited script.

If it’s okay, I think it’s better for me to figure it out while testing it inside of your game with how the scripts are set up. With permission, could you provide (us) a place file?

1 Like

Absolutely.

I only have one other script running in the game, which is just changing the walk animation, but you won’t really see it. I did edit the freefall time to ‘1’ to make sure that the animation plays no matter what and to ensure that it’s working. Maybe that’s it?
test.rbxl (39.0 KB)

It can also be that how I scripted it was poorly written… Though with me going through the trials and errors I should be able to find a good solution so, thank you for providing us with the place file! I’ll work on it ASAP, whenever RStudio finishes updating. LOL

1 Like

HAHA NO PROBLEM!! RStudio is a bit special when it comes to updating, so thank you so much! Also, hey, we’re all learning through scripting so it’s a-okay! (:

1 Like

THERE’S ACTUALLY A TOPIC THAT TALKS ABOUT FINDING HOW LONG A PLAYER HAS BEEN FALLING FOR. Maybe it can be useful for you if you want to script it on your own without waiting for me.

1 Like

OHHHH!!! I could, but I’m still a bit amateur at scripting, so having someone a bit more experienced than me tackling this is very much preferred! I’m very patient at waiting, so I’m okay with waiting!

Okay so i rewrote my script’s elseif statement when the player lands, since im not the creator of your animation it won’t play your animation for me anyways so i used one that i made. When tested it works perfectly to what i assume you need

local Char = script.Parent
local Hum:Humanoid = Char:WaitForChild("Humanoid")
local Animator: Animator = Hum:WaitForChild("Animator")

local playAnim: AnimationTrack = Animator:LoadAnimation(script:WaitForChild("DoPlay"))-- DoPlay is the name of the animation under the script, change the animationid to what you want

local freefallTime = 1

local start = tick() -- Initially set tick(), so as to not break the script when the player respawns

Hum.StateChanged:Connect(function(_, state)
	if state == Enum.HumanoidStateType.Jumping or state == Enum.HumanoidStateType.Freefall then
		start = tick() -- Start tracking as soon as player enter's jumping or freefall states
	elseif Hum:GetState() ~= Enum.HumanoidStateType.Freefall then
		local endTime = tick()
		if (endTime - start) > freefallTime then -- compare
			playAnim:Play()
		end
	end
end)

Edit: You should also use the animator object when loading animations

3 Likes