Load animation to skinned mesh

i’m trying to load animation to skinned mesh and make it an npc but
the animation is either weird or won’t work at all
i tried to use the animate script from roblox, i tried to find an animated mesh npc in toolbox for reference, i also tried to make animation script myself

local function playAnimation(animationid)
	local anim = Instance.new("Animation")
	anim.AnimationId = animationid
	animtrack = animator:LoadAnimation(anim)
	animtrack:Play()
end

playAnimation(walk)
playAnimation(idle)

but nothing worked… and i have no idea why

can someone please explain me how to load an animation to a skinned mesh?

Are you sure that you have the animator variable defined correctly? If so, try and add print() statements:

local function playAnimation(animationid)
    print("Function fired")
	local anim = Instance.new("Animation")
	anim.AnimationId = animationid
    print("Creating & Setting Animation")
	animtrack = animator:LoadAnimation(anim)
	animtrack:Play()
    print("Playing Animation")
end

playAnimation(walk)
playAnimation(idle)

i mean, my script is playing the animation, but it playing only the walking animation

Might be because the Priority is set like that? Try temporary removing the playAnimation(walk) line?

now it’s playing the idle animation only

Wait, couldn’t you just detect changes made by the Humanoid when they either walk or stay idle?

wdym?
if i don’t remove the line, they play the walking animation even when they are idle

Like, detecting the Humanoid’s State Type of when your NPC is walking or staying idle

Could you try this…?

local function playAnimation(animationid)
	local anim = Instance.new("Animation")
	anim.AnimationId = animationid
	animtrack = animator:LoadAnimation(anim)
	animtrack:Play()
end

print("Does the script even work ._.")

local Humanoid = script.Parent:WaitForChild("Humanoid")
print(Humanoid)
print("???")

Humanoid.StateChanged:Connect(function(Old, NewState)
    print("State Changed")
    if NewState == Enum.HumanoidStateType.Running then
        print("NPC is walking")
        playAnimation(walk)
    else
        print("NPC is still")
        playAnimation(idle)
    end
end)

that’s not working, it won’t even enter the function

The script should be parented to your NPC, correct? There should also be a Humanoid object inside it

yes, it parented to the NPC and there’s Humanoid inside it

Ok now this is the part where I start to just edit my script That’s strange, could you try it again and see what gets outputted?

nothing gets outputted, it’s empty

Ok, maybe Humanoid is defined as nil or something? I know I have the event right, so it couldn’t be that

oh, i found out the issue, i accidently put it under a loop lol so it didn’t reach that code, but the animation still has some issues, it still always playing the run animation ;-;

okay, here’s what i did,

local animtrack = nil
local animator = script.Parent.Humanoid.Animator
local Humanoid = script.Parent:WaitForChild("Humanoid")

local function playAnimation(animation)
	animtrack = animator:LoadAnimation(script[animation])
	animtrack:Play()
end

local function stop()
	if animtrack then
		animtrack:Stop()	
	end
end

Humanoid.StateChanged:Connect(function(Old, NewState)
	if NewState == Enum.HumanoidStateType.Running then
		stop()
		print("NPC is walking")
		playAnimation("walk")
	else
		stop()
		print("NPC is still")
		playAnimation("idle")
	end
end)

the problem is, statechanged not firing always when state changed