Help with animation code!

This is an update to my last post. (THE CODE IS AI)
I had deep seek AI create code for a model i made and animation i made to go with the model. both were made in blender and i imported it to Roblox fine, so i know it works but when i comes to the code i don’t know what I’m doing wrong or if I’ve placed something in the wrong place. for some reason i wasn’t able to weld the mesh and the HumanoidRootPart so i had ai code a part where it welds both parts, dont know if that affects anything. any feedback is appreciated!

the code: (AI)

– COMPLETE AI SCRIPT - WITH ANIMATOR FIX
– Place this in your mesh model

– ========== CONFIGURATION ==========
local ANIMATION_ID = “99520322741814”
local DETECTION_RANGE = 50
local MOVEMENT_SPEED = 16
local ATTACK_RANGE = 5

– ========== SERVICES ==========
local Players = game:GetService(“Players”)
local RunService = game:GetService(“RunService”)

– ========== SETUP MESH ==========
local meshModel = script.Parent

print(“=== STARTING SETUP ===”)

– CRITICAL: Keep animation system
if not meshModel:FindFirstChild(“AnimationController”) then
Instance.new(“AnimationController”).Parent = meshModel
end

– ========== FIND YOUR TEXTURED MESH FIRST ==========
local texturedMesh = meshModel:FindFirstChild(“Meshy_AI_Sludge_Monster_0130223256_texture”)
if not texturedMesh then
– Try to find any mesh
texturedMesh = meshModel:FindFirstChildWhichIsA(“MeshPart”) or
meshModel:FindFirstChildWhichIsA(“Part”)
if texturedMesh then
print("Found mesh: " .. texturedMesh.Name)
else
warn(“ERROR: No mesh found!”)
return
end
end

– Keep your mesh visible
texturedMesh.Transparency = 0 – Make sure it’s visible
texturedMesh.CanCollide = false – Don’t interfere with movement
texturedMesh.Anchored = false – Can follow root

print(“Textured mesh: " .. texturedMesh.Name .. " (Visible)”)

– ========== CREATE SEPARATE HUMANOIDROOTPART ==========
local rootPart = meshModel:FindFirstChild(“HumanoidRootPart”)

if not rootPart then
– Create NEW invisible HumanoidRootPart
rootPart = Instance.new(“Part”)
rootPart.Name = “HumanoidRootPart”
rootPart.Size = Vector3.new(4, 4, 4) – Collision size
rootPart.Transparency = 1 – INVISIBLE!
rootPart.CanCollide = true – Won’t fall through floor
rootPart.Anchored = false
rootPart.Position = texturedMesh.Position – Start at mesh position
rootPart.Parent = meshModel

print("Created invisible HumanoidRootPart")

else
print(“Using existing HumanoidRootPart”)
end

– ========== CREATE HUMANOID WITH ANIMATOR ==========
local humanoid = meshModel:FindFirstChildOfClass(“Humanoid”)
if not humanoid then
humanoid = Instance.new(“Humanoid”)
humanoid.Name = “Humanoid”
humanoid.WalkSpeed = MOVEMENT_SPEED
humanoid.Parent = meshModel
print(“Created Humanoid”)
end

– ========== CRITICAL: ADD ANIMATOR TO HUMANOID ==========
local animator = humanoid:FindFirstChildOfClass(“Animator”)
if not animator then
animator = Instance.new(“Animator”)
animator.Parent = humanoid
print(“Created Animator”)
end

– ========== WELD MESH TO ROOT ==========
– Remove any old welds
for _, child in ipairs(rootPart:GetChildren()) do
if child:IsA(“WeldConstraint”) and child.Part1 == texturedMesh then
child:Destroy()
end
end

– Create new weld
local weld = Instance.new(“WeldConstraint”)
weld.Name = “MeshWeld”
weld.Part0 = rootPart – INVISIBLE root (receives animation)
weld.Part1 = texturedMesh – VISIBLE mesh (shows animation)
weld.Parent = rootPart

print(“Welded " .. texturedMesh.Name .. " to HumanoidRootPart”)

– ========== SET PRIMARY PART ==========
meshModel.PrimaryPart = rootPart

print(“=== Setup Complete ===”)
print("Humanoid: " .. tostring(humanoid ~= nil))
print("Animator: " .. tostring(animator ~= nil))
print(“HumanoidRootPart: " .. rootPart.Name .. " (Invisible)”)
print(“Textured Mesh: " .. texturedMesh.Name .. " (Visible)”)
print("Animation ID: " .. ANIMATION_ID)

– ========== LOAD AND PLAY ANIMATION USING ANIMATOR ==========
print(“Loading animation using Animator…”)

– Create animation
local animation = Instance.new(“Animation”)
animation.AnimationId = “rbxassetid://” .. ANIMATION_ID
animation.Name = “walk” – Standard name helps

– ========== FIXED: Use Animator instead of Humanoid ==========
local animationTrack
local success, errorMessage = pcall(function()
– Try loading with Animator (recommended way)
animationTrack = animator:LoadAnimation(animation)
animationTrack:Play()
animationTrack.Looped = true
animationTrack.Priority = Enum.AnimationPriority.Core – Force it to play
return animationTrack
end)

if success and animationTrack then
print(“SUCCESS: Animation loaded via Animator!”)
print("Track Name: " .. animationTrack.Name)
print("IsPlaying: " .. tostring(animationTrack.IsPlaying))
print("Priority: " .. tostring(animationTrack.Priority))
else
print("WARNING: Animator method failed: " .. tostring(errorMessage))

-- Fallback to old method (just in case)
print("Trying fallback with Humanoid...")
animationTrack = humanoid:LoadAnimation(animation)
animationTrack:Play()
animationTrack.Looped = true
animationTrack.Priority = Enum.AnimationPriority.Core
print("Fallback animation loaded")

end

– Monitor animation
task.spawn(function()
while true do
task.wait(3)
if animationTrack and not animationTrack.IsPlaying then
print(“Restarting animation…”)
animationTrack:Play()
animationTrack.Looped = true
end
end
end)

– ========== SIMPLE AI ==========
local targetPlayer = nil
local isChasing = false

– Find nearest player
local function findNearestPlayer()
for _, player in ipairs(Players:GetPlayers()) do
local character = player.Character
if character and character:FindFirstChild(“HumanoidRootPart”) then
local distance = (rootPart.Position - character.HumanoidRootPart.Position).Magnitude
if distance < DETECTION_RANGE then
return player
end
end
end
return nil
end

– ========== KILL ON MESH TOUCH ==========
texturedMesh.Touched:Connect(function(otherPart)
local character = otherPart.Parent
local victimHumanoid = character:FindFirstChild(“Humanoid”)
if victimHumanoid then
victimHumanoid.Health = 0
print("Killed: " .. character.Name)
end
end)

print(“Kill zone: Textured mesh”)

– Main AI loop
RunService.Heartbeat:Connect(function()
if not rootPart or humanoid.Health <= 0 then return end

if not isChasing then
	targetPlayer = findNearestPlayer()
	if targetPlayer then
		isChasing = true
		print("Chasing: " .. targetPlayer.Name)
	end
end

if isChasing and targetPlayer then
	local character = targetPlayer.Character
	if not character then
		isChasing = false
		return
	end

	local targetRoot = character:FindFirstChild("HumanoidRootPart")
	if not targetRoot then
		isChasing = false
		return
	end

	local distance = (rootPart.Position - targetRoot.Position).Magnitude

	if distance > DETECTION_RANGE * 10 then
		isChasing = false
		humanoid:MoveTo(rootPart.Position)
	else
		humanoid:MoveTo(targetRoot.Position)
	end
end

end)

– Reset when player leaves
Players.PlayerRemoving:Connect(function(player)
if player == targetPlayer then
isChasing = false
targetPlayer = nil
end
end)

print(“=== AI SYSTEM READY ===”)
print("Animation: " .. (animationTrack and animationTrack.IsPlaying and “PLAYING” or “NOT PLAYING”))
print("Using: " .. (animator and “ANIMATOR” or “HUMANOID”))
print(“Mesh should be VISIBLE and animating!”)

First thing I noticed is that the humanoid does not have an animator object and the code does not use that. I believe humanoid:LoadAnimation() is deprecated, and it should be Humanoid.Animator:LoadAnimation(). This may not be the problem, but its simple and cant hurt to try

1 Like

thank you, i will defienetly add it and see what happens :slight_smile:

just edited and reuploaded the code and pretty much had the same outcome.