Identify issue with animation script

Can someone identify the issue with this script (I’m new, Sorry if its obvious)

local player = game.Players.LocalPlayer
local ani = Instance.new("Animation")
ani.AnimationId = "6213269139"

game:GetService("UserInputService").InputBegan:Connect(function(input, event)
	if input.KeyCode == Enum.KeyCode.F and not event then
		local playAni = player.Character:WaitForChild("Humanoid"):LoadAnimation(ani)
		playAni:Play()
	end
end)

Note: Its a LocalScript in StarterPlayerScripts, I tried starter character scripts.
Heres the error when I try to press the designated key.
image

In animations, if you type a number into the id section, it will change to add rblxasset or something in front.

First put those numbers into the AnimationId section, wait for it to convert into a string, then copy that into the script

EDITED.
What @Mad_Scientist99 is true, and animations are assets. Animation IDs, as well as sound, mesh IDs etc. should be accompanied with “rbxassetid://”. In the following code, I also moved animation loading outside function, because the animation has to be loaded only once. Another change is the object we are loading animation to. humanoid:LoadAnimation() has been deprecated in favour of animator:LoadAnimation(). Both methods work, although animator is a descendant of humanoid, so in newer work, loading of animations should be performed on it directly.

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")

if (not Players.LocalPlayer.Character) then Players.LocalPlayer.CharacterAdded:Wait() end
local humanoid = Players.LocalPlayer.Character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://6213269139"
local playAnim = animator:LoadAnimation(anim)

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.F and not gameProcessed then
		playAnim:Play()
	end
end)

EDIT.
@octav20071 thank you for your reply! That is indeed the issue, although I believe you accidentally replied to me, otherwise, the solution has most likely already been found, but the original poster hasn’t yet responded.

EDIT (2)
@akai_shi1 are you sure animation ID is correct? Did you set animation priority?

image

1 Like

I think you should change ani.AnimationId = “6213269139” to ani.AnimationId = “rbxassetid://6213269139”

edit: oops ^^

I tried throwing that into a local script, no errors when I go to press the key nothing happened.

Ah there we go, Thank you. One last question: Its a sitting animation how would I keep the player setting on the ground until a key is pressed? ( No need to answer just wondering)

1 Like

EDIT (2021-03-07)

@akai_shi1 try this code. It is changed a little bit, but consists of two parts.

Part one is a server script that belongs into your seat. It checks for occupants and loads animations to them.

seat = script.Parent

local anim = Instance.new("Animation")
anim.AnimationId = "rbxassetid://6213269139"

local _animTrack

seat:GetPropertyChangedSignal("Occupant"):Connect(function()	
	local occupant = seat.Occupant
	if (occupant ~= nil) then
		local animator = occupant:FindFirstChild("Animator")
		if (animator) then
			_animTrack = animator:LoadAnimation(anim)
			_animTrack:Play()
		end
	else
		_animTrack:Stop()
	end
end)

The other part belongs in StarterCharacterScripts. Each time player is sits down, the script prevents player from jumping until they press F (or any other key). It works no matter which seat it is. I used ContextActionService instead of UserInputService, because you can bind actions according to context that way. Pressing F (or other key), while player is not sitting doesn’t have any effect in this case.

local CAS = game:GetService("ContextActionService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

if (not Players.LocalPlayer.Character) then Players.LocalPlayer.CharacterAdded:Wait() end
local humanoid = Players.LocalPlayer.Character:WaitForChild("Humanoid")

local connection

local function preventJumping(actionName, inputState, inputObject)
	connection:Disconnect()
	CAS:UnbindAction("preventJumping")
	humanoid.Sit = false; humanoid.Jump = true
	return Enum.ContextActionResult.Pass
end

humanoid:GetPropertyChangedSignal("Sit"):Connect(function()
	RunService.Heartbeat:Wait() -- Wait for state to change.
	if (humanoid:GetState() == Enum.HumanoidStateType.Seated) then
		CAS:BindAction("preventJumping", preventJumping, false, Enum.KeyCode.F)
		connection = humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
			humanoid.Jump = false
		end)
	end
end)
1 Like

Thank you for that, I tried it but the character stood up, It might be something with the animation not sure.

1 Like

Make sure your animation is set to loop

It’s not like an actual sitting animation its like a sitting emote, I’m wondering how to keep them setting?