Error while loading animation

This is my script for a crouch system. All of a sudden, it stopped working and keeps giving me this error "LoadAnimation requires the Animator object (Animator) to be a descendant of the game object". I don’t know what is wrong. Help

Server:
game.Players.PlayerAdded:connect(function(plr)
	repeat wait() until plr.Character
	local animator = Instance.new("Animator",plr.Character.Humanoid)
end)

Client:
local UserInputService = game:GetService("UserInputService")
local character = game.Players.LocalPlayer.Character
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator", 10)
local isCrawling = false
while character.Parent == nil do
	character.AncestryChanged:wait()
end
repeat wait() until animator
local crawlAnim = animator:LoadAnimation(script.Crouch)
repeat wait() until character.Parent.Name == "Human"

humanoid.Running:Connect(function(speed)
	if speed < 1 then
		crawlAnim:Stop()
		humanoid.WalkSpeed = 16
		humanoid.JumpPower = 50
		isCrawling = false
		script.Parent.Parent.Parent.Parent.Human.Backpack.List.Visible = true
	end
end)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		if not isCrawling then
			isCrawling = true
			crawlAnim:Play()
			crawlAnim:AdjustSpeed(0)
			humanoid.WalkSpeed = 4
			humanoid.JumpPower = 0
			script.Parent.Parent.Parent.Parent.Human.Backpack.List.Visible = false
		else
			crawlAnim:Stop()
			humanoid.WalkSpeed = 16
			humanoid.JumpPower = 50
			isCrawling = false
			script.Parent.Parent.Parent.Parent.Human.Backpack.List.Visible = true
		end
	end
end)

script.Parent.MouseButton1Click:Connect(function()
	if not isCrawling then
		isCrawling = true
		crawlAnim:Play()
		humanoid.WalkSpeed = 4
		humanoid.JumpPower = 0
		script.Parent.Parent.Parent.Parent.Human.Backpack.List.Visible = false
	else
		crawlAnim:Stop()
		humanoid.WalkSpeed = 16
		humanoid.JumpPower = 50
		isCrawling = false
		script.Parent.Parent.Parent.Parent.Human.Backpack.List.Visible = true
	end
end)

Which line is causing the error?

Hey,
Where is this Local script located?
If it’s not in StarterCharacterScripts, the character might not exist yet.

To get the character for sure, I usually do this:

local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait() -- if the character doesn't exist, wait until it does.

It sounds like whether the character doesn’t exist, or the Animator doesn’t exist.
to be a descendant of the game - It means it is probably parented to nil (destroyed or removed)

I am not quite sure, but I think the Animator object is automatically generated

1 Like