Issue with playing animation when reaching certain health

I’m making it so when a player has below 30 health an animation plays but its not working my output says

I got an animation in rep storage

Unable to cast value to Object

Script:

local humanoid = character:FindFirstChild("Humanoid")
local animationId = "9738175727"
local loadedAnimation = humanoid:LoadAnimation(animationId)

while wait() do
	if humanoid.Health <= 30 then
		loadedAnamation:Play()
	else
		loadedAnimation:stop()
	end
end

This is happening because to load a animation, it expects a animation instance, not a Id.

something like this?

local animation = game.ReplicatedStorage :FindFirstChild(“LowHealthAnim”)

Yes.
(character limit, character limit)

Yes. I also think you need to play the animation on the humanoid’s animator.

what do you mean by humanoids animator? and also will i delete the ID in the script

use Humanoid.HealthChanged

local Humanoid = character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")

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

local loadedAnimation = Animator:LoadAnimation(Animation)

Humanoid.HealthChanged:Connect(function(Health)
	if Health <= 30 then
		loadedAnamation:Play()
	else
		loadedAnimation:stop()
	end
end)

The animator is what is mainly responsible for playing animations on characters. You can get the animator from the humanoid.

Do i keep the animation thing in replicated storage

also output said

also character is red underlined

Workspace.3DRobloxGameDev.LowHealth:1: attempt to index nil with ‘WaitForChild’

Put that inside localscript in startergui or where it was

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()

local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")

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

local loadedAnimation = Animator:LoadAnimation(Animation)

Humanoid.HealthChanged:Connect(function(Health)
	if Health <= 30 then
		loadedAnamation:Play()
	else
		loadedAnimation:stop()
	end
end)
1 Like

Players.3DRobloxGameDev.PlayerGui.LowHealth:4: attempt to index nil with ‘Character’

is LowHealth script a LocalScript?

1 Like

This does know work thank you but the health icon on the right top corner of the screen don’t show

image
if you mean health bar it displays when health below max health

yeah that isn’t showing for me

it might be because you have script that disables health bar
this is example of it

StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)

No not what I could see but I do have a health display script that don’t work

err… maybe because of this? or you already fixed it? (typo)

yeah this is fixed i noticed that though just

1 Like

try changing this

to

local animationId = "rbxassetid://9738175727"

and this

to

local loadedAnimation = humanoid:WaitForChild("Animator"):LoadAnimation(animationId)