I want to play an animation from a bundle!

Hi !
I have made a script that play an animation when I press a button and when I print the animation playing that print me the animation, but I didn’t see anything :

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local SwimmingAnimation = Instance.new("Animation")
SwimmingAnimation.AnimationId = "http://www.roblox.com/asset/?id="..1090133583
local SwimmingAnimationTrack = Animator:LoadAnimation(SwimmingAnimation)
SwimmingAnimationTrack.Priority = Enum.AnimationPriority.Action4
SwimmingAnimationTrack.Looped = true

Humanoid.StateChanged:Connect(function(OldState, NewState)
	if NewState == Enum.HumanoidStateType.Swimming then
		Humanoid.WalkSpeed = 12
		Humanoid.JumpHeight = 0
	elseif OldState == Enum.HumanoidStateType.Swimming then
		Humanoid.WalkSpeed = 8
		Humanoid.JumpHeight = 4
	end
end)

UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		if Humanoid.WalkSpeed == 8 then
			Humanoid.WalkSpeed = 20
		elseif Humanoid.WalkSpeed == 12 then
			Humanoid.WalkSpeed = 18
			SwimmingAnimationTrack:Play()
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input, GameProcessedEvent)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		if Humanoid.WalkSpeed == 20 then
			Humanoid.WalkSpeed = 8
		elseif Humanoid.WalkSpeed == 18 then
			Humanoid.WalkSpeed = 12
			SwimmingAnimationTrack:Stop()
		end
	end
end)

Thanks you for the reply and have a nice day !

2 Likes

I just tested this script with my own animation and it worked fine for me. These are a few reasons why it may not be working for you:

  • In your games, you can only play animations that were created by your account, or created by roblox themselves. If the animation you are using was made by someone else, that will explain why this is not working, and it should work fine with your own animation.

  • Make sure that the animation you are trying to play is the same body type as your character. For example, if you’re making a game with R15 avatars, make sure the animation was made for an R15 avatar.

  • Your condition for the animation playing is that the player is walking at speed 12. This may be intentional and you have your character moving at speed 12 when you want this to happen, but just reminding you that by default players walk at speed 16, so you may want to change the condition to:

elseif Humanoid.WalkSpeed == 16 then

If none of those are the reason, please reply with any error messages you get in the output.

1 Like

Thanks you for the reply but I have already resolve my problem, I think I can only play animation made by me :

But it is a roblox animation …
So I have paste the animation in the animation editor and I have export it and now it work !
Have a nice day !

1 Like

Glad you got it working. An extra thing I’d do is add this line of code to the top of your input began and input ended functions:

if GameProcessedEvent then return end

GameProcessedEvent is true when the player is in a menu such as typing in chat, so I’d recommend adding that line so that the functions don’t run when the player is using those keys to type.

1 Like

I have do this :

UserInputService.InputBegan:Connect(function(Input, GameProcessedEvent)
	if Input.KeyCode == Enum.KeyCode.LeftShift and not GameProcessedEvent then
		if Humanoid.WalkSpeed == 6 then
			Humanoid.WalkSpeed = 20
		elseif Humanoid.WalkSpeed == 12 then
			Humanoid.WalkSpeed = 16
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input, GameProcessedEvent)
	if Input.KeyCode == Enum.KeyCode.LeftShift and not GameProcessedEvent then
		if Humanoid.WalkSpeed == 20 then
			Humanoid.WalkSpeed = 6
		elseif Humanoid.WalkSpeed == 16 then
			Humanoid.WalkSpeed = 12
		end
	end
end)

THANKS YOU SO MUCH UNTIL NOW I DIDN’T UNDERSTAND WHAT IT WAS !!!
Have a nice day !

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.