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

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(.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




