Click for start animation problem

Hello!
Today I made an animation in the moon animator, I exported the animation and wrote a script. Its purpose is when the player presses F the animation starts playing.

This is how it looks:

local UserInputService = game:GetService("UserInputService")
local Animation = script.Animation

UserInputService.InputBegan(function(Input)
	if Input.KeyCode ==Enum.KeyCode.F then
		local Player = game.Players.LocalPlayer
		local Character = Player.Character
		local Humanoid =Character:WaitForChild("Humanoid")
		
		local Anim = Humanoid:LoadAnimation("Ascend")
		Anim:Play()
	end
end)

This is a local script that is a child of StartPlayerScripts.Output says that Animation is not valid member of LocalScripts.

Bye! :wave:

1 Like
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Animation = script:WaitForChild("Animation")
local Anim = Humanoid:LoadAnimation(Animation)

UIS.InputBegan(function(Input, Processed)
	if Processed then
		return
	end
	if Input.KeyCode == Enum.KeyCode.F then
		Anim:Play()
	end
end)

Okay, so first of all declare constants (variables with values which are not subject to change) outside of the callback connected to the event otherwise you just end up redeclaring variables and assigning them the same values (whenever the event is fired), second of all you create a reference to the animation you want to play but then call “LoadAnimation()” and pass a string value to it which is invalid, “LoadAnimation()” expects an Animation instance to be passed as an argument to it and in return it will provide the loaded AnimationTrack instance. I’ve also made use of the additional argument of “InputBegan” which allows us to ignore inputs which are in relation to the core game mechanics/core gui and as such were likely not intended to trigger the event.

1 Like

Try out

local UserInputService = game:GetService(“UserInputService”)
local Animation = script:WaitForChild(“Animation”) – put Waitforchild

1 Like

I found another script and it works, the problem is solved