Having trouble with animations

I’ve never scripted animations before, so this is my first time doing it but I am unfortunately struggling. I’ve been going at it with this script for like a solid week and I’ve made no progress. I’ve even tried to see if ChatGPT could help me understand what I’m doing wrong, but it’s no good either.

local StarterPlayer = game:GetService("StarterPlayer")
local humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")

local animationIds = {
	beingRevived = "rbxassetid://13956810525",
	reviving = "rbxassetid://13956955414",
	downedIdle = "rbxassetid://13956860997",
	downedMoving = "rbxassetid://13956866584",
	crouchIdle = "rbxassetid://13956850946",
	crouchMoving = "rbxassetid://13956854919",
	crawlIdle = "rbxassetid://13956843888",
	crawlMoving = "rbxassetid://13956846671",
	jump = "rbxassetid://13956888748",
	holdingObject = "rbxassetid://13956937717",
	holdingFlashlight = "rbxassetid://13956934376",
	highSanityIdle = "rbxassetid://13956872221",
	highSanityWalk = "rbxassetid://13956883194",
	highSanityRun = "rbxassetid://13956877253",
	midSanityIdle = "rbxassetid://13956912463",
	midSanityWalk = "rbxassetid://13956916712",
	lowSanityIdle = "rbxassetid://13956899990",
	lowSanityWalk = "rbxassetid://13956908609",
	lowSanityRun = "rbxassetid://13956904936"
}

local animationNames = {
	beingRevived = "BeingRevived",
	reviving = "Reviving",
	downedIdle = "DownedIdle",
	downedMoving = "DownedMoving",
	crouchIdle = "CrouchIdle",
	crouchMoving = "CrouchMoving",
	crawlIdle = "CrawlIdle",
	crawlMoving = "CrawlMoving",
	jump = "Jump",
	holdingObject = "HoldingObject",
	holdingFlashlight = "HoldingFlashlight",
	highSanityIdle = "HighSanityIdle",
	highSanityWalk = "HighSanityWalk",
	highSanityRun = "HighSanityRun",
	midSanityIdle = "MidSanityIdle",
	midSanityWalk = "MidSanityWalk",
	midSanityRun = "MidSanityRun",
	lowSanityIdle = "LowSanityIdle",
	lowSanityWalk = "LowSanityWalk",
	lowSanityRun = "LowSanityRun"
}


local function playAnimation(animationId, humanoid)
	local track = humanoid:LoadAnimation(animationId)
	track:Play()
end


local sanityValue = humanoid:GetAttribute("Sanity") or 0
local sanityChangedSignal = Instance.new("BindableEvent")

local function setSanity(value)
	sanityValue = value
	sanityChangedSignal:Fire(value)
end

local function getSanity()
	return sanityValue
end

local function playJumpAnimation()
	playAnimation(animationIds.jump, humanoid)
end

local function checkSanityAndPlayAnimation()
	local currentSanity = getSanity()

	if currentSanity and currentSanity >= 80 then
		if humanoid.WalkSpeed == 0 then
			playAnimation(animationIds.highSanityIdle, humanoid)
		elseif humanoid.WalkSpeed > 5 or not humanoid:FindFirstChild("IsRunning") then
			playAnimation(animationIds.highSanityWalk, humanoid)
		elseif humanoid.WalkSpeed > 10 or humanoid:FindFirstChild("IsRunning") then
			playAnimation(animationIds.highSanityRun, humanoid)
		end
	elseif currentSanity and currentSanity >= 65 then
		-- Mid Sanity Animations
		playAnimation(animationIds.midSanityIdle, humanoid)
	elseif currentSanity and currentSanity < 65 then
		-- Low Sanity Animations
		playAnimation(animationIds.lowSanityIdle, humanoid)
	end
end



humanoid.StateChanged:Connect(function(_, newState)
	if newState == Enum.HumanoidStateType.Freefall then
		playJumpAnimation()
	elseif newState == Enum.HumanoidStateType.Running or newState == Enum.HumanoidStateType.RunningNoPhysics then
		checkSanityAndPlayAnimation()
	end
end)

humanoid.AttributeChanged:Connect(function(attribute)
	if attribute == "Sanity" then
		local newSanityValue = humanoid:GetAttribute(attribute)
		setSanity(newSanityValue)
	end
end)

checkSanityAndPlayAnimation()
sanityChangedSignal.Event:Connect(checkSanityAndPlayAnimation)

I’ve re-done this script around 50 times, so if you think I should abolish this script too and make a new one, please let me know. Sending Animation tutorial videos can help aswell.

For context clues: My friend has made these animations and uploaded them to his account (I don’t know if that’s one reason why this script won’t work).

One of the errors is the Animations don’t work. This is a horror game and when your sanity is higher a certain walk, idle, and run animation is supposed to run for you. Same applies for the Mid and Lower sanity animations. But none of those work. I have my Sanity set as a Attribute (NumberValue) in my humanoid, but I also don’t know why if that’s one of the reasons why this script refuses to work.
image

Another issue is Line 52:
image
image

This is why I’m wondering if this has something to do with the animations being loaded onto my friend’s account.

Suggestions?

2 Likes

humanoid:LoadAnimation() takes an Animation, not an animation id

local function playAnimation(animationId, humanoid)
    local anim = Instance.new('Animation')
    anim.AnimationId = animationId
	local track = humanoid:LoadAnimation(anim)
	track:Play()
end

hopefully i spelt everything right and this solves, something

also as far as i know you’re now recommended to use humanoid.Animator:LoadAnimation(), not humanoid:LoadAnimation(). An animator usually ads itself in but, if not then u can add one into the humanoid.

Instead of making a new Animation every time however, you should probably pre-make them all using a loop and a table, then load them in when you need to. You can probably figure that out on your own.

Last I checked, using an animation you do not own will not work in game, but may work in studio. Aside from core roblox animations.

He must be using Animator to load animations, AnimationClipProvidor would bug out if he used the :LoadAnimator() function on the Humanoid.

PS: Loading animations requires the animation object, not the animation ID

Last I checked, using an animation you do not own will not work in game, but may work in studio. Aside from core roblox animation

So what if I bought the animations? It’ll work in-game?

Yes and you save them to roblox or have your dev or whatever send you the rig he did the animations on and you can just get the animations from there in “AnimSaves”, you save them to roblox and done, you own them. That’s if its made with roblox animator or moon animator, I’m not quite sure if it’s the same way with blender but I suppose it’s around there. (I believe you only need to save them to your roblox so you own them)

2 Likes

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