local g=script.Parent local R=g.Slime:LoadAnimation(g.Slime.Animation)R:Play()g.Slime.Died:Connect(function()g:Destroy()end)local Z=false local function j(g)if not Z then local R=g.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if R then R.Health-=damage Z=true wait(damageDebounceDelay)Z=false end end end for g,R in pairs(g:GetChildren())do if R:IsA("\066\097\115\101\080\097\114\116")and R.Name~="\072\105\116\066\111\120"then R.Touched:Connect(j)end end local function O(g)local R=false local Z=999999999 for j,O in pairs(game.Players:GetChildren())do local w=(game.Workspace:WaitForChild(O.Name)).HumanoidRootPart.Position local h=(g-w).magnitude if h<Z then Z=h R=game.Workspace:WaitForChild(O.Name)end end return R end local w=false while true do wait(.1)local R=O(g.Head.Position)if R then local Z=(R.PrimaryPart.Position-g.HumanoidRootPart.Position).magnitude if Z<detectionDistance then w=false g.Slime.WalkSpeed=chaseSpeed g.Slime:MoveTo(R.PrimaryPart.Position)else if not w then w=true g.Slime.WalkSpeed=wanderSpeed g.Slime:MoveTo(g.HumanoidRootPart.Position+Vector3.new(math.random(-wanderDistance,wanderDistance),0,math.random(-wanderDistance,wanderDistance)))g.Slime.MoveToFinished:Connect(function()w=false end)end end end end
--------- Slim AI Script (don't edit unless you know what you're doing) -----------
local slime = script.Parent
local walkAnimation = slime.Slime:LoadAnimation(slime.Slime.Animation)

walkAnimation:Play()

slime.Slime.Died:Connect(function()
	slime:Destroy()
	-- TODO: Add fancier death or give rewards
end)

local hasDamagedRecently = false
local function damageIfHumanoidDebounced(part)
	if not hasDamagedRecently then
		local humanoid = part.Parent:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Health -= damage
			hasDamagedRecently = true
			wait(damageDebounceDelay)
			hasDamagedRecently = false
		end
	end
end
for _, part in pairs(slime:GetChildren()) do
	if part:IsA("BasePart") and part.Name ~= "HitBox" then
		part.Touched:Connect(damageIfHumanoidDebounced)
	end
end

local function getClosestPlayer(position)
	local closestPlayer = false
	local closestDistance = 999999999
	for _, player in pairs(game.Players:GetChildren()) do
		local playerPosition = game.Workspace:WaitForChild(player.Name).HumanoidRootPart.Position
		local dist = (position - playerPosition).magnitude
		if dist < closestDistance then
			closestDistance = dist
			closestPlayer = game.Workspace:WaitForChild(player.Name)
		end
	end
	return closestPlayer
end

local isWandering = false
while true do -- main AI loop
	wait(0.1)
	local player = getClosestPlayer(slime.Head.Position)
	if player then
		local distance = (player.PrimaryPart.Position - slime.HumanoidRootPart.Position).magnitude
		if distance < detectionDistance then
			isWandering = false
			slime.Slime.WalkSpeed = chaseSpeed
			slime.Slime:MoveTo(player.PrimaryPart.Position)
		else
			if not isWandering then
				isWandering = true
				slime.Slime.WalkSpeed = wanderSpeed
				slime.Slime:MoveTo(
					slime.HumanoidRootPart.Position
						+ Vector3.new(
							math.random(-wanderDistance, wanderDistance),
							0,
							math.random(-wanderDistance, wanderDistance)
						)
				)
				slime.Slime.MoveToFinished:Connect(function()
					isWandering = false
				end)
			end
		end
	end
end
