How to apply an animation to a character

I am making a grill for my game, and during the cooking process, I want the game to take control of the character. How would I play the animation?

Disable player movement assuming you want them to not move
Load and Play an animation
https://developer.roblox.com/en-us/resources/build-it-play-it/2020summer/tier-2/animations-and-feedback/adding-animations

1 Like

I want it as part of the script. Here is the script:

local function animateFridge(plr, fridge)
	local fridgeAnimator = script.Parent.FridgeCore.AnimationController.Animator
	local openAnimation = script.Parent.FridgeCore.Animations.DoorOpen
	local closeAnimation = script.Parent.FridgeCore.Animations.DoorClose
	
	local openAnimTrack = fridgeAnimator:LoadAnimation(openAnimation)
	
	local controls = require(plr.PlayerScripts.PlayerModule):GetControls()
	
	local character = plr.Character
	if not character or character.Parent then
		character = plr.CharacterAdded:Wait()
	end
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")
	local reachForDrink = Instance.new("Animation")
	reachForDrink.AnimationId = "rbxassetid://9154371617"
	local reachForDrinkTrack = animator:LoadAnimation(reachForDrink)
	
	local closeAnimTrack = fridgeAnimator:LoadAnimation(closeAnimation)
	
	controls:Disable()
	openAnimTrack:Play()
	openAnimTrack.Stopped:Wait(0.5)
	reachForDrinkTrack:Play()
	reachForDrinkTrack.Stopped:Wait(0.5)
	closeAnimTrack:Play()
end

Don’t worry about most of that stuff, what I’m focused on is this:

	local controls = require(plr.PlayerScripts.PlayerModule):GetControls()

It’s saying that Player scripts isn’t part of the player, what do I do?

Here is usually what I do to play a Animation on a Humanoid.

local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://0"
Anim.Name = "Anim"
Anim.Looped = true <----- I believe this is a property

local Character = player.Character
local Humanoid = player.Character:FindFirstChildWhichIsA("Humanoid")

local track = Humanoid:LoadAnimation(Anim)
track:Play()

And for the controls, maybe put a wait at the beginning of the script, if it’s occurring when the player loads in. Or can I see the error you get for the requiring?

local controls = require(plr.PlayerScripts.PlayerModule):GetControls()

^ Make sure the Module Script is loaded into PlayerScripts of the player when they join, best way of doing that would just to put it in starterscripts.

and when Calling for a function in a module typically you’d do,

local controls = require(plr.PlayerScripts.PlayerModule)

and then later on when you need a function from the module do,

local function = controls.functionHere(x)