local q=script.Parent local n=q.Humanoid:LoadAnimation(q.Humanoid.Animation)n:Play()q.Humanoid.Died:Connect(function()q:Destroy()end)local g=false local function M(q)if not g then local n=q.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if n then n.Health-=damage g=true wait(damageDebounceDelay)g=false end end end for q,n in pairs(q:GetChildren())do if n:IsA("\066\097\115\101\080\097\114\116")and n.Name~="\072\105\116\066\111\120"then n.Touched:Connect(M)end end local function N(q)local n=false local g=999999999 for M,N in pairs(game.Players:GetChildren())do local d=(game.Workspace:WaitForChild(N.Name)).HumanoidRootPart.Position local x=(q-d).magnitude if x<g then g=x n=game.Workspace:WaitForChild(N.Name)end end return n end local d=false while true do wait(.1)local n=N(q.Head.Position)if n then local g=(n.PrimaryPart.Position-q.HumanoidRootPart.Position).magnitude if g<detectionDistance then d=false q.Humanoid.WalkSpeed=chaseSpeed q.Humanoid:MoveTo(n.PrimaryPart.Position)else if not d then d=true q.Humanoid.WalkSpeed=wanderSpeed q.Humanoid:MoveTo(q.HumanoidRootPart.Position+Vector3.new(math.random(-wanderDistance,wanderDistance),0,math.random(-wanderDistance,wanderDistance)))q.Humanoid.MoveToFinished:Connect(function()d=false end)end end end end

local slime = script.Parent
local walkAnimation = slime.Humanoid:LoadAnimation(slime.Humanoid.Animation)
walkAnimation:Play()

slime.Humanoid.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(.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.Humanoid.WalkSpeed = chaseSpeed
			slime.Humanoid:MoveTo(player.PrimaryPart.Position)			
		else
			if not isWandering then
				isWandering = true
				slime.Humanoid.WalkSpeed = wanderSpeed
				slime.Humanoid:MoveTo(slime.HumanoidRootPart.Position + Vector3.new(math.random(-wanderDistance, wanderDistance), 0, math.random(-wanderDistance, wanderDistance)))
				slime.Humanoid.MoveToFinished:Connect(function()
					isWandering = false
				end)
			end
		end		
	end
end




