Playing animations for a humanoid NPC

Hello. I can not get animations to play when called from a local script inside the monster model. Sorry for the messy code.

local monster = script.Parent
local RS = game:GetService("ReplicatedStorage")
local debuggerEvent = RS:WaitForChild("DebuggerEvent")

local humanoid = monster:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local walkTrack = animator:LoadAnimation(monster:WaitForChild("WalkAnim"))
local isPlaying = false
debuggerEvent:FireServer("Loaded")

local lastPosition = monster.PrimaryPart.Position

while monster:GetAttribute("AIActive") == true do
	debuggerEvent:FireServer("Loop Running")
	local currentPosition = monster.PrimaryPart.Position
	local distanceMoved = (currentPosition - lastPosition).Magnitude
	local isMoving = distanceMoved > 0.1
	lastPosition = currentPosition
	debuggerEvent:FireServer(isMoving, isPlaying)
	if isMoving then
		if isPlaying == false then
			walkTrack:Play()
			isPlaying = true
		end
	else
		if isPlaying == true then
			walkTrack:Stop()
			isPlaying = false
		end
	end
	task.wait(0.2)
end

local RS = game:GetService("ReplicatedStorage")
local debuggerEvent = RS:WaitForChild("DebuggerEvent")

debuggerEvent.OnServerEvent:Connect(function(player, stage, stage1)
	print(player)
	print(stage, stage1)
	
end)

It does not even fire the events. Do local scripts not work when they are not under player? If yes, then how should I handle the animation playing?

If the localscript is located in the monster model (assuming it’s in workspace) it will not work because localscripts can only run in StarterPlayer, StarterGui, ReplicatedStorage/First, etc. Maybe try using a serverscript with its runcontext set to client